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

Desktop: Resolves #1752: Added capability to toggle visibility of the Menu Bar from the View menu #10324

Merged

Conversation

LightTreasure
Copy link
Contributor

@LightTreasure LightTreasure commented Apr 18, 2024

Add menu option and shortcut to toggle visibility of the menu bar

What this does:

This PR adds a new item to the View Menu called "Toggle Menu Bar", and proposes Ctrl+Shift+M as its default keyboard shortcut. Clicking on this item or using the shortcut toggles the visibility of the menu bar. Also, when the bar is hidden, pressing Alt will temporarily show the menu bar
EDIT: The menu item is not shown on Mac since macOS does not allow for hiding menus.

This feature has been requested via the following issues:

It has also been requested on the Joplin Forum:

How implemented:

Builds on @tinyoverflow's previously submitted pull request to address the same issue. This work incorporates @laurent22's feedback in that PR to add this capability as a View Menu item instead of it being present on the settings page.

  • Added a new entry to the 'View' Menu, called "Toggle menu bar", with a proposed keyboard shortcut Ctrl+Shift+M (defined in lib/services/KeymapService.ts), and a localization string added to tools/locales/joplin.pot and english translation tools/locales/en_US.po
  • Added a new, non-public setting called hideMenuBar, a boolean with the default value of false, applicable only to the Desktop App (defined in lib/models/Setting.ts)
  • Created a MainScreen Command called toggleMenuBar(), which contains the main implementation. This flips the value of the hideMenuBar setting and then correspondingly calls setAutoHideMenuBar() and setMenuBarVisibility() on the Electron Window.
  • Added a similar function called updateMenuBar() in app-desktop/app.ts - Called during initializatoin, this function calls setAutoHideMenuBar() and setMenuBarVisibility() based on the value of the hideMenuBar setting.
  • toggleMenuBar() and updateMenuBar() do not do anything on Mac.

Usage with screenshots:

The new menu entry:
joplin_menuentry

When the setting is toggled:
joplin_nomenu

Toggling again brings the menu back:
joplin_start

Issues:

Sometimes, after hiding the menu bar, the window is left with a white empty area, indicating that the contents aren't painted correctly. @tinyoverflow also mentioned this issue in their PR. I've tried debugging this, but haven't been able to fix it. My guess is that Electron isn't repainting the window properly after the menu bar is hidden.
joplin_nomenu_whitearea

Copy link
Contributor

github-actions bot commented Apr 18, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@LightTreasure
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Apr 18, 2024
Comment on lines 4941 to 4944
#: packages/app-desktop/gui/MainScreen/commands/toggleMenuBar.ts:9
msgid "Toggle menu bar"
msgstr ""

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to add this here - that will be done automatically as long as you wrap the translatable strings in _(...) which you did

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this in the latest commit

Comment on lines 21 to 23
// Now apply the setting
electronWindow.setAutoHideMenuBar(Setting.value('hideMenuBar'));
electronWindow.setMenuBarVisibility(!Setting.value('hideMenuBar'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove, since you're already doing this in the reducer middleware

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this file and the references to this command in the latest commit

Comment on lines 1286 to 1291
hideMenuBar: {
value: false,
type: SettingItemType.Bool,
public: false,
appTypes: [AppType.Desktop],
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry would you mind naming it "showMenuBar" and setting it to true by default? This is the pattern we try to follow to avoid double-negations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in the latest commit.

Comment on lines 208 to 215
public updateMenuBar() {
// macOS disallows the removal of the menu bar
if (shim.isMac()) return;

const window = bridge().window();
window.setAutoHideMenuBar(Setting.value('hideMenuBar'));
window.setMenuBarVisibility(!Setting.value('hideMenuBar'));
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a MenuBar component to handle the menu. Would you mind moving the logic over there? If you need some help implementing this please let me know.

Essentially you'll need to add this to mapStateToProps:

showMenuBar: state.settings.showMenuBar,

Then check the value of props.showMenuBar from the component and hide/show it based on this.

The reason for doing it that way is to encapsulate all menu bar related logic in the same place, and to avoid the need to sync the settings and the state in the middleware.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Makes sense - I'll try implementing it this way and reply back here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the implementation to the MenuBar component in the latest commit. That simplifies things a lot!

…omponent, and changed the name of the setting to "showMenuBar"
Copy link
Owner

@laurent22 laurent22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there but please bring back the command you had created

@@ -190,6 +191,15 @@ function menuItemSetEnabled(id: string, enabled: boolean) {
menuItem.enabled = enabled;
}

function applyMenuBarVisibility(props: Props) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const applyMenuBarVisibility = (props:Props => { (see coding style guide)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And actually only pass to the function that variables that you need - so just "showMenuBar", not the whole props

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. Fixed.

Comment on lines 773 to 781
shim.isMac() ? noItem : {
label: _('Toggle menu bar'),
accelerator: keymapService.getAccelerator('toggleMenuBar'),
click: () => {
Setting.setValue('showMenuBar', !Setting.value('showMenuBar'));
applyMenuBarVisibility(props);
},
},
// shim.isMac() ? noItem : menuItemDic.toggleMenuBar,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but I'm not sure why you removed your command? That was the correct way to implement this, since it would be a command that can also be triggered from user scripts. It will also automatically register the keyboard shortcut.

Once the command is created, you can add it to menuCommandName.ts and then add it to the menu using the menuItemDic.xxxx pattern (see other commands in the menu)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies - I clicked on "View Reviewed changes" in GitHub. For some reason, in that view, GitHub doesn't show which lines the comment applied to. I assumed you meant that I should remove the entire file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored the command and used it for the implementation.

Comment on lines 4654 to 4657
#: packages/app-desktop/gui/MenuBar.tsx:774
msgid "Toggle menu bar"
msgstr ""

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove - this will be done automatically

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Not very familiar with this style of localization. Removed it.

Copy link
Owner

@laurent22 laurent22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good now, thanks @LightTreasure. I'd just remove the isMac check in the command since it's redundant

Comment on lines 14 to 15
// Defensive code: macOS disallows hiding the menu bar, so ignore this Command
if (shim.isMac()) return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this stage it's not necessary to handle this, since you already handle it where it matters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yup that makes sense. Removed in the newest commit.

@LightTreasure
Copy link
Contributor Author

Hi @laurent22, please let me know if you have any further suggestions. Thanks so much!

@laurent22
Copy link
Owner

That looks good now, thanks @LightTreasure!

@laurent22 laurent22 merged commit 548ba7d into laurent22:dev May 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants