Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to allow quitting with open tabs #1268

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion porcupine/menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,15 @@ def focus_active_tab() -> None:
set_enabled_based_on_tab("File/Save", (lambda tab: isinstance(tab, tabs.FileTab)))
set_enabled_based_on_tab("File/Save As", (lambda tab: isinstance(tab, tabs.FileTab)))
set_enabled_based_on_tab("File/Close", (lambda tab: tab is not None))
set_enabled_based_on_tab("File/Quit", (lambda tab: tab is None))

callback = set_enabled_based_on_tab(
"File/Quit",
(lambda tab: tab is None or global_settings.get("allow_quit_with_open_tabs", bool)),
)

# Update quit menu item disabled-ness whenever main window is focused.
# It is easier than accessing the menu item when the checkbox is checked/unchecked, and seems to work.
get_main_window().bind("<FocusIn>", callback, add=True)

def change_font_size(how: Literal["bigger", "smaller", "reset"]) -> None:
if how == "reset":
Expand Down
37 changes: 26 additions & 11 deletions porcupine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,10 @@ def update_fixedfont(event: tkinter.Event[tkinter.Misc] | None) -> None:
)
update_fixedfont(None)

global_settings.add_option("allow_quit_with_open_tabs", default=False)

def _create_dialog_content() -> ttk.Frame:

def _create_dialog_content() -> tuple[ttk.Frame, ttk.Frame]:
dialog = tkinter.Toplevel()
dialog.withdraw()
dialog.title("Porcupine Settings")
Expand All @@ -504,7 +506,9 @@ def confirm_and_reset_all() -> None:
big_frame = ttk.Frame(dialog)
big_frame.pack(fill="both", expand=True)
content = ttk.Frame(big_frame)
content.pack(fill="both", expand=True, padx=5, pady=5)
content.pack(fill="x", padx=5, pady=5)
checkbox_frame = ttk.Frame(big_frame)
checkbox_frame.pack(fill="both", expand=True, padx=5, pady=5)
ttk.Separator(big_frame).pack(fill="x")
buttonframe = ttk.Frame(big_frame, padding=5)
buttonframe.pack(fill="x")
Expand All @@ -516,10 +520,11 @@ def confirm_and_reset_all() -> None:

content.grid_columnconfigure(0, weight=1)
content.grid_columnconfigure(1, weight=1)
return content
return (content, checkbox_frame)


_dialog_content: ttk.Frame | None = None
_checkbox_frame: ttk.Frame | None = None


def show_dialog() -> None:
Expand All @@ -546,7 +551,7 @@ def get_dialog_content() -> ttk.Frame:
Use grid with the returned widget. Its columns are configured like this::

,-----------------------------------------------------------.
| Porcupine Settings | _ | O | X |
| Porcupine Settings | O | X |
|-----------------------------------------------------------|
| : : |
| : :col|
Expand All @@ -567,9 +572,9 @@ def get_dialog_content() -> ttk.Frame:
| : : |
| : : |
|===========================================================|
| ,---------. ,---------. |
| | Reset | | OK | |
| `---------' `---------' |
| ,----------------------. ,---------. |
| | Reset all settings | | OK | |
| `----------------------' `---------' |
`-----------------------------------------------------------'

Column 0 typically contains labels such as "Font Family:", and column 1
Expand Down Expand Up @@ -687,8 +692,9 @@ def setup() -> None:
Currently it is not possible to display a |triangle| next to the
checkbutton. Let me know if you need it.
"""
checkbutton = ttk.Checkbutton(get_dialog_content(), **checkbutton_kwargs)
checkbutton.grid(column=0, columnspan=2, sticky="w", pady=2)
assert _checkbox_frame is not None
checkbutton = ttk.Checkbutton(_checkbox_frame, **checkbutton_kwargs)
checkbutton.pack(fill="x")

var = tkinter.BooleanVar()

Expand Down Expand Up @@ -964,14 +970,23 @@ def _fill_dialog_content_with_defaults() -> None:
)
add_pygments_style_button("pygments_style", "Pygments style for editing:")

checkbox = add_checkbutton("allow_quit_with_open_tabs")
checkbox.bind(
"<Map>",
lambda event: checkbox.config(
text=f"Allow quitting with {utils.get_binding('<<Menubar:File/Quit>>')} when there are no open tabs"
),
)


# undocumented on purpose, don't use in plugins
def init_the_rest_after_initing_enough_for_using_disabled_plugins_list() -> None:
global _dialog_content
assert _dialog_content is None
global _checkbox_frame
assert _dialog_content is None and _checkbox_frame is None

_log.debug("initializing continues")
_init_global_gui_settings()
_dialog_content = _create_dialog_content()
_dialog_content, _checkbox_frame = _create_dialog_content()
_fill_dialog_content_with_defaults()
_log.debug("initialized")