add a new data to track the most recently opened/saved files, and update recent files menu as the recent files change
This commit is contained in:
parent
e2fba18ba5
commit
6bb1d5cba2
3 changed files with 22 additions and 5 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
@ -167,14 +171,15 @@ class MenuAction:
|
|||
num_files = len(recent_files)
|
||||
if num_files == 0:
|
||||
recent_files.insert(0, file_path)
|
||||
elif 0 < num_files <= 3:
|
||||
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 == 3:
|
||||
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()
|
||||
|
|
|
@ -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,6 +426,13 @@ class Menubar(tk.Menu):
|
|||
else:
|
||||
logging.warning("File does not exist %s", filename)
|
||||
|
||||
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:
|
||||
|
|
Loading…
Add table
Reference in a new issue