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:
Huy Pham 2020-02-04 13:49:46 -08:00
parent e2fba18ba5
commit 6bb1d5cba2
3 changed files with 22 additions and 5 deletions

View file

@ -94,6 +94,7 @@ def check_directory():
}, },
"servers": [{"name": "example", "address": "127.0.0.1", "port": 50051}], "servers": [{"name": "example", "address": "127.0.0.1", "port": 50051}],
"nodes": [], "nodes": [],
"recentfiles": [],
"observers": [{"name": "hello", "cmd": "echo hello"}], "observers": [{"name": "hello", "cmd": "echo hello"}],
} }
save(config) 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.dialogs.throughput import ThroughputDialog
from core.gui.task import BackgroundTask from core.gui.task import BackgroundTask
MAX_FILES = 3
if TYPE_CHECKING: if TYPE_CHECKING:
from core.gui.app import Application from core.gui.app import Application
@ -78,6 +80,7 @@ class MenuAction:
defaultextension=".xml", defaultextension=".xml",
) )
if file_path: if file_path:
self.add_recent_file_to_gui_config(file_path)
self.app.core.save_xml(file_path) self.app.core.save_xml(file_path)
def file_open_xml(self, event: tk.Event = None): def file_open_xml(self, event: tk.Event = None):
@ -93,6 +96,7 @@ class MenuAction:
def open_xml_task(self, filename): def open_xml_task(self, filename):
if filename: if filename:
self.add_recent_file_to_gui_config(filename)
self.app.core.xml_file = filename self.app.core.xml_file = filename
self.app.core.xml_dir = str(os.path.dirname(filename)) self.app.core.xml_dir = str(os.path.dirname(filename))
self.prompt_save_running_session() self.prompt_save_running_session()
@ -167,14 +171,15 @@ class MenuAction:
num_files = len(recent_files) num_files = len(recent_files)
if num_files == 0: if num_files == 0:
recent_files.insert(0, file_path) recent_files.insert(0, file_path)
elif 0 < num_files <= 3: elif 0 < num_files <= MAX_FILES:
if file_path in recent_files: if file_path in recent_files:
recent_files.remove(file_path) recent_files.remove(file_path)
recent_files.insert(0, file_path) recent_files.insert(0, file_path)
else: else:
if num_files == 3: if num_files == MAX_FILES:
recent_files.pop() recent_files.pop()
recent_files.insert(0, file_path) recent_files.insert(0, file_path)
else: else:
logging.error("unexpected number of recent files") logging.error("unexpected number of recent files")
self.app.save_config() 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.master.config(menu=self)
self.app = app self.app = app
self.menuaction = action.MenuAction(app, master) self.menuaction = action.MenuAction(app, master)
self.recent_menu = None
self.draw() self.draw()
def draw(self): def draw(self):
@ -58,9 +59,12 @@ class Menubar(tk.Menu):
menu.add_command(label="Reload", underline=0, state=tk.DISABLED) menu.add_command(label="Reload", underline=0, state=tk.DISABLED)
self.app.bind_all("<Control-s>", self.save) self.app.bind_all("<Control-s>", self.save)
# some hard code values for testing self.recent_menu = tk.Menu(menu)
recent = tk.Menu(menu) for i in self.app.guiconfig["recentfiles"]:
menu.add_cascade(label="Recent files", menu=recent) 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_separator()
menu.add_command(label="Export Python script...", state=tk.DISABLED) menu.add_command(label="Export Python script...", state=tk.DISABLED)
menu.add_command(label="Execute XML or 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: else:
logging.warning("File does not exist %s", filename) 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): def save(self, event=None):
xml_file = self.app.core.xml_file xml_file = self.app.core.xml_file
if xml_file: if xml_file: