Skip to content

Commit 18c7bc8

Browse files
Show recent projects
1 parent a865736 commit 18c7bc8

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

gui.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ def create_config(self) -> None:
145145
if not self.load_key("unix_drive_mount_point"):
146146
self.save_key("unix_drive_mount_point", "/media")
147147

148+
def add_recent_project(self, path: Path) -> None:
149+
"""
150+
Add a project to the recent category.
151+
152+
:param path: The path of the .cpypmconfig file.
153+
:return: None.
154+
"""
155+
self.save_key("last_dir_opened", str(path.parent.parent))
156+
recent_projects = self.load_key("opened_recent")
157+
if recent_projects is None:
158+
recent_projects = []
159+
if str(path) in recent_projects:
160+
recent_projects.pop(recent_projects.index(str(path)))
161+
recent_projects = [Path(p) for p in recent_projects]
162+
while len(recent_projects) > 10:
163+
recent_projects.pop()
164+
recent_projects.insert(0, str(path))
165+
self.save_key("opened_recent", [str(p) for p in recent_projects])
166+
self.update_recent_projects()
167+
148168
def open_project(self) -> None:
149169
"""
150170
Open a project.
@@ -162,7 +182,7 @@ def open_project(self) -> None:
162182
logger.debug(f"Returned valid path! Path is {repr(path)}")
163183
self.cpypmconfig_path = path
164184
self.update_main_gui()
165-
self.save_key("last_dir_opened", str(path.parent.parent))
185+
self.add_recent_project(path)
166186
else:
167187
logger.debug("User canceled opening project!")
168188

@@ -380,6 +400,8 @@ def create_new_project(self) -> None:
380400
self.update_main_gui()
381401
self.disable_closing = False
382402
self.dismiss_dialog(self.new_project_window)
403+
self.add_recent_project(self.cpypmconfig_path)
404+
self.update_recent_projects()
383405

384406
def open_create_new_project(self) -> None:
385407
"""
@@ -396,6 +418,22 @@ def open_create_new_project(self) -> None:
396418
self.create_new_project_buttons()
397419
self.new_project_frame.wait_window()
398420

421+
def update_recent_projects(self) -> None:
422+
"""
423+
Update the opened recent projects menu.
424+
425+
:return: None.
426+
"""
427+
self.opened_recent_menu.delete(0, tk.END)
428+
self.recent_projects = self.load_key("opened_recent")
429+
if self.recent_projects is None:
430+
self.recent_projects = []
431+
self.recent_projects = [Path(p) for p in self.recent_projects]
432+
for path in self.recent_projects:
433+
self.opened_recent_menu.add_command(label=str(path), command=None)
434+
if len(self.recent_projects) == 0:
435+
self.opened_recent_menu.add_command(label="No recent projects!", state=tk.DISABLED)
436+
399437
def create_file_menu(self) -> None:
400438
"""
401439
Create the file menu.
@@ -407,6 +445,9 @@ def create_file_menu(self) -> None:
407445
self.file_menu.add_command(label="New...", command=self.open_create_new_project, underline=0)
408446
self.file_menu.add_command(label="Open...", command=self.open_project, underline=0)
409447
# TODO: Add open recent
448+
self.opened_recent_menu = tk.Menu(self.file_menu)
449+
self.file_menu.add_cascade(label="Open recent", menu=self.opened_recent_menu, underline=5)
450+
self.update_recent_projects()
410451
self.file_menu.add_command(label="Close project", command=self.close_project, underline=0)
411452
self.file_menu.add_separator()
412453
self.file_menu.add_command(label="Exit", command=self.try_to_close, underline=0)
@@ -420,9 +461,9 @@ def create_edit_menu(self) -> None:
420461
self.edit_menu = tk.Menu(self.menu_bar)
421462
self.menu_bar.add_cascade(menu=self.edit_menu, label="Edit", underline=0)
422463
self.edit_menu.add_command(label="Open .cpypmconfig",
423-
command=lambda: open_application(self.cpypmconfig_path), underline=6)
464+
command=lambda: open_application(str(self.cpypmconfig_path)), underline=6)
424465
self.edit_menu.add_command(label="Open .cpypmconfig file location",
425-
command=lambda: open_application(self.cpypmconfig_path.parent), underline=23)
466+
command=lambda: open_application(str(self.cpypmconfig_path.parent)), underline=23)
426467
self.edit_menu.add_separator()
427468
self.edit_menu.add_command(label="Save changes", command=self.save_modified, underline=0)
428469
self.edit_menu.add_command(label="Discard changes", command=self.discard_modified, underline=0)
@@ -914,6 +955,8 @@ def create_gui(self, cpypmconfig_path: Path = None) -> None:
914955
self.create_config()
915956
self.create_menu()
916957
self.make_main_gui(cpypmconfig_path)
958+
if cpypmconfig_path is not None:
959+
self.add_recent_project(cpypmconfig_path)
917960

918961
def run(self, cpypmconfig_path: Path = None) -> None:
919962
"""

0 commit comments

Comments
 (0)