Merge pull request #366 from coreemu/coretk-enhance/fix-bug

Coretk enhance/fix bug
This commit is contained in:
bharnden 2020-02-05 10:32:22 -08:00 committed by GitHub
commit 19680bd452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View file

@ -94,6 +94,7 @@ def check_directory():
},
"servers": [{"name": "example", "address": "127.0.0.1", "port": 50051}],
"nodes": [],
"recentfiles": [],
"observers": [{"name": "hello", "cmd": "echo hello"}],
}
save(config)

View file

@ -22,6 +22,8 @@ from core.gui.dialogs.sessions import SessionsDialog
from core.gui.dialogs.throughput import ThroughputDialog
from core.gui.task import BackgroundTask
MAX_FILES = 3
if TYPE_CHECKING:
from core.gui.app import Application
@ -78,6 +80,7 @@ class MenuAction:
defaultextension=".xml",
)
if file_path:
self.add_recent_file_to_gui_config(file_path)
self.app.core.save_xml(file_path)
def file_open_xml(self, event: tk.Event = None):
@ -93,6 +96,7 @@ class MenuAction:
def open_xml_task(self, filename):
if filename:
self.add_recent_file_to_gui_config(filename)
self.app.core.xml_file = filename
self.app.core.xml_dir = str(os.path.dirname(filename))
self.prompt_save_running_session()
@ -161,3 +165,21 @@ class MenuAction:
def config_throughput(self):
dialog = ThroughputDialog(self.app, self.app)
dialog.show()
def add_recent_file_to_gui_config(self, file_path):
recent_files = self.app.guiconfig["recentfiles"]
num_files = len(recent_files)
if num_files == 0:
recent_files.insert(0, file_path)
elif 0 < num_files <= MAX_FILES:
if file_path in recent_files:
recent_files.remove(file_path)
recent_files.insert(0, file_path)
else:
if num_files == MAX_FILES:
recent_files.pop()
recent_files.insert(0, file_path)
else:
logging.error("unexpected number of recent files")
self.app.save_config()
self.app.menubar.update_recent_files()

View file

@ -24,6 +24,7 @@ class Menubar(tk.Menu):
self.master.config(menu=self)
self.app = app
self.menuaction = action.MenuAction(app, master)
self.recent_menu = None
self.draw()
def draw(self):
@ -58,9 +59,12 @@ class Menubar(tk.Menu):
menu.add_command(label="Reload", underline=0, state=tk.DISABLED)
self.app.bind_all("<Control-s>", self.save)
# some hard code values for testing
recent = tk.Menu(menu)
menu.add_cascade(label="Recent files", menu=recent)
self.recent_menu = tk.Menu(menu)
for i in self.app.guiconfig["recentfiles"]:
self.recent_menu.add_command(
label=i, command=partial(self.open_recent_files, i)
)
menu.add_cascade(label="Recent files", menu=self.recent_menu)
menu.add_separator()
menu.add_command(label="Export Python script...", state=tk.DISABLED)
menu.add_command(label="Execute XML or Python script...", state=tk.DISABLED)
@ -422,7 +426,14 @@ class Menubar(tk.Menu):
else:
logging.warning("File does not exist %s", filename)
def save(self):
def update_recent_files(self):
self.recent_menu.delete(0, tk.END)
for i in self.app.guiconfig["recentfiles"]:
self.recent_menu.add_command(
label=i, command=partial(self.open_recent_files, i)
)
def save(self, event=None):
xml_file = self.app.core.xml_file
if xml_file:
self.app.core.save_xml(xml_file)