From 740185beabcef9cb5b382fab5e28fb057a7fa172 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 15 Aug 2023 16:26:42 +0800 Subject: [PATCH 1/7] Initial running version on Windows - Ugly `cfg` used to disable MacOS-specific code. - `apply_mica` vibrancy looks so bad. - Shortcut's broken. Will investigate soon. This commit is made using commit!! --- src/main.rs | 7 ++++++- src/window/main_window.rs | 4 +++- src/window/mod.rs | 23 +++++++++++++++++------ src/window/settings.rs | 17 ++++++++++++++++- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e6448b..cd3786f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,11 @@ use anyhow::anyhow; use config::{Config, ConfigExt}; use std::error::Error; -use tauri::{generate_context, ActivationPolicy, Builder as Tauri}; +use tauri::{generate_context, Builder as Tauri}; + +#[cfg(target_os = "macos")] +use tauri::ActivationPolicy; + use tauri_autostart::ManagerExt; use tauri_plugin_autostart::{self as tauri_autostart}; @@ -33,6 +37,7 @@ fn main() { } fn setup_tauri(app: &mut tauri::App) -> Result<(), Box<(dyn Error + 'static)>> { + #[cfg(target_os = "macos")] app.set_activation_policy(ActivationPolicy::Accessory); window::main_window::create(&app.handle())?; diff --git a/src/window/main_window.rs b/src/window/main_window.rs index 9cc7042..7169334 100644 --- a/src/window/main_window.rs +++ b/src/window/main_window.rs @@ -37,7 +37,9 @@ pub fn hide(window: &Window) -> Result<(), tauri_plugin_spotlight::Error> { pub fn on_open(window: Window) { shortcuts::register_escape(window.clone()).unwrap(); - shortcuts::register_settings(&window.app_handle()).unwrap(); + shortcuts::register_settings(&window.app_handle()).unwrap_or_else(|_| { + eprintln!("Failed to register settings shortcut"); + }); tauri::async_runtime::spawn(async move { let app = window.app_handle(); diff --git a/src/window/mod.rs b/src/window/mod.rs index a25b567..2853ff7 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -2,8 +2,13 @@ use tauri::{ plugin::TauriPlugin, AppHandle, GlobalWindowEvent, Manager, RunEvent, Window, WindowEvent, }; use tauri_plugin_spotlight::{PluginConfig, WindowConfig}; + +#[cfg(target_os = "macos")] use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial, NSVisualEffectState}; +#[cfg(target_os = "windows")] +use window_vibrancy::apply_mica; + use crate::shortcuts; pub const MAIN: &str = "main"; @@ -66,11 +71,17 @@ pub trait TransparentWindow { impl TransparentWindow for Window { fn make_transparent(&self) -> Result<(), window_vibrancy::Error> { - apply_vibrancy( - self, - NSVisualEffectMaterial::HudWindow, - Some(NSVisualEffectState::Active), - Some(10.0), - ) + { + #[cfg(target_os = "macos")] + apply_vibrancy( + self, + NSVisualEffectMaterial::HudWindow, + Some(NSVisualEffectState::Active), + Some(10.0), + ) + } + + #[cfg(target_os = "windows")] + apply_mica(self, Some(false)) } } diff --git a/src/window/settings.rs b/src/window/settings.rs index 5cb76d2..3ebc775 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -1,11 +1,14 @@ use anyhow::anyhow; -use tauri::{AppHandle, Manager, TitleBarStyle, Window, WindowBuilder, WindowUrl}; +use tauri::{AppHandle, Manager, Window, WindowBuilder, WindowUrl}; +#[cfg(target_os = "macos")] +use tauri::TitleBarStyle; use crate::window; use super::TransparentWindow; pub fn create(app: &AppHandle) -> anyhow::Result { + #[cfg(target_os = "macos")] let settings_window = WindowBuilder::new(app, window::SETTINGS, WindowUrl::App(Default::default())) .visible(false) @@ -17,6 +20,18 @@ pub fn create(app: &AppHandle) -> anyhow::Result { .initialization_script("window.__COMMIT__ = { page: 'settings' };") .build()?; + // not macos + #[cfg(not(target_os = "macos"))] + let settings_window = + WindowBuilder::new(app, window::SETTINGS, WindowUrl::App(Default::default())) + .visible(false) + .closable(true) + .transparent(true) + .always_on_top(true) + .title("Settings - Commit") + .initialization_script("window.__COMMIT__ = { page: 'settings' };") + .build()?; + settings_window.make_transparent().map_err(|_| { anyhow!("Unsupported platform! 'apply_vibrancy' is only supported on macOS") })?; From 09a7bfc59922f9c2fc498b397f02195f97124fe4 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 15 Aug 2023 16:53:21 +0800 Subject: [PATCH 2/7] Shortcut fix - Now default shortcut for Windows is usable. - blur effect is broken as well. - Settings shortcut looks broken. --- src/shortcuts.rs | 13 +++++++++++-- src/window/mod.rs | 5 +++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/shortcuts.rs b/src/shortcuts.rs index 84c54e4..a069080 100644 --- a/src/shortcuts.rs +++ b/src/shortcuts.rs @@ -2,7 +2,16 @@ use tauri::{AppHandle, GlobalShortcutManager, Manager, Window}; use crate::window; +// todo: maybe use `cfg-if` crate for many platform-specific stuff +#[cfg(target_os = "macos")] pub const DEFAULT_SHORTCUT: &str = "Cmd+Alt+Shift+C"; +#[cfg(target_os = "macos")] +pub const SETTINGS_SHORTCUT: &str = "Cmd+,"; + +#[cfg(target_os = "windows")] +pub const DEFAULT_SHORTCUT: &str = "Ctrl+Alt+Shift+C"; +#[cfg(target_os = "windows")] +pub const SETTINGS_SHORTCUT: &str = "Ctrl+Shift+,"; pub fn update_default( app: &AppHandle, @@ -28,7 +37,7 @@ pub fn register_settings(app: &AppHandle) -> Result<(), anyhow::Error> { let mut shortcuts = app.global_shortcut_manager(); let settings_window = window::settings::get(app).unwrap(); - shortcuts.register("Cmd+,", move || { + shortcuts.register(SETTINGS_SHORTCUT, move || { settings_window.show().unwrap(); settings_window.set_focus().unwrap(); })?; @@ -39,7 +48,7 @@ pub fn register_settings(app: &AppHandle) -> Result<(), anyhow::Error> { pub fn unregister_settings(app: &AppHandle) -> Result<(), anyhow::Error> { let mut shortcuts = app.global_shortcut_manager(); - shortcuts.unregister("Cmd+,")?; + shortcuts.unregister(SETTINGS_SHORTCUT)?; Ok(()) } diff --git a/src/window/mod.rs b/src/window/mod.rs index 2853ff7..fcf8947 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -7,7 +7,7 @@ use tauri_plugin_spotlight::{PluginConfig, WindowConfig}; use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial, NSVisualEffectState}; #[cfg(target_os = "windows")] -use window_vibrancy::apply_mica; +use window_vibrancy::apply_blur; use crate::shortcuts; @@ -81,7 +81,8 @@ impl TransparentWindow for Window { ) } + // not working #[cfg(target_os = "windows")] - apply_mica(self, Some(false)) + apply_blur(self, Some((18, 18, 18, 125))) } } From 2f524c30c0ed1b7478e3008d501735c64676dcc7 Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 16 Aug 2023 05:00:12 +0200 Subject: [PATCH 3/7] Run Windows on CI --- .github/workflows/build.yaml | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1a3d75a..3156f74 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -7,7 +7,18 @@ on: jobs: build: - runs-on: macos-latest + strategy: + fail-fast: false + matrix: + settings: + - host: macos-latest + name: macOS + targets: app,dmg + - host: windows-latest + name: Windows + targets: msi,nsis + name: Build for ${{ matrix.settings.name }} + runs-on: ${{ matrix.settings.host }} steps: - name: Checkout the code uses: actions/checkout@v3 @@ -49,8 +60,11 @@ jobs: - name: Build the app uses: tauri-apps/tauri-action@v0 + with: + args: --bundles ${{ matrix.settings.targets }} - name: 🚀 Upload macOS dmg + if: matrix.settings.host == 'macos-latest' uses: actions/upload-artifact@v3 with: name: Commit.dmg @@ -58,8 +72,17 @@ jobs: target/release/bundle/dmg/Commit*.dmg - name: 🚀 Upload macOS App + if: matrix.settings.host == 'macos-latest' uses: actions/upload-artifact@v3 with: name: Commit.app path: | target/release/bundle/macos/ + + - name: 🚀 Upload Windows App + if: matrix.settings.host == 'windows-latest' + uses: actions/upload-artifact@v3 + with: + name: Commit.exe + path: | + target/release/bundle/windows/Commit.exe From e6b232bf9d5281d1dcd12076e06841d5162886d7 Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 16 Aug 2023 05:20:06 +0200 Subject: [PATCH 4/7] Actually upload Windows build results to CI --- .github/workflows/build.yaml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3156f74..3f09e34 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -71,7 +71,7 @@ jobs: path: | target/release/bundle/dmg/Commit*.dmg - - name: 🚀 Upload macOS App + - name: 🚀 Upload macOS app if: matrix.settings.host == 'macos-latest' uses: actions/upload-artifact@v3 with: @@ -79,10 +79,18 @@ jobs: path: | target/release/bundle/macos/ - - name: 🚀 Upload Windows App + - name: 🚀 Upload Windows NSIS if: matrix.settings.host == 'windows-latest' uses: actions/upload-artifact@v3 with: name: Commit.exe path: | - target/release/bundle/windows/Commit.exe + target/release/bundle/nsis/Commit*.exe + + - name: 🚀 Upload Windows MSI + if: matrix.settings.host == 'windows-latest' + uses: actions/upload-artifact@v3 + with: + name: Commit.exe + path: | + target/release/bundle/msi/Commit*.msi From 05e7a21ca042c63b3c9fbea7b881941b9882d0af Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 16 Aug 2023 05:56:20 +0200 Subject: [PATCH 5/7] whoops, wrong name --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3f09e34..3e6e48d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -91,6 +91,6 @@ jobs: if: matrix.settings.host == 'windows-latest' uses: actions/upload-artifact@v3 with: - name: Commit.exe + name: Commit.msi path: | target/release/bundle/msi/Commit*.msi From ecdc798c5654d2580590bf77bafa411cd5624ad1 Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 16 Aug 2023 06:07:01 +0200 Subject: [PATCH 6/7] Use CommandOrControl for keyboard shortcuts --- frontend/src/components/ShortcutPicker.tsx | 8 ++------ src/shortcuts.rs | 12 ++---------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/frontend/src/components/ShortcutPicker.tsx b/frontend/src/components/ShortcutPicker.tsx index 3b00714..4a18178 100644 --- a/frontend/src/components/ShortcutPicker.tsx +++ b/frontend/src/components/ShortcutPicker.tsx @@ -28,10 +28,6 @@ const ShortcutPicker: FC = ({ value, onChange }) => { setShortcut(parseShortcut(value)) }, [value]) - // useEffect(() => { - // onChange(buildShortcut(shortcut)) - // }, [shortcut]) - const handleKeyDown = useCallback( (event: KeyboardEvent) => { event.preventDefault() @@ -75,10 +71,10 @@ const parseShortcut = (shortcut: string): ParsedShortcut => { return { isAlt: parts.includes('Alt'), - isCmd: parts.includes('Cmd'), isCtrl: parts.includes('Ctrl'), isShift: parts.includes('Shift'), natural: parts[parts.length - 1].toUpperCase(), + isCmd: parts.includes('CommandOrControl') || parts.includes('Cmd'), } } @@ -100,10 +96,10 @@ const displayShortcut = (shortcut: ParsedShortcut): string => { const buildShortcut = (shortcut: ParsedShortcut): string => { let parts = [] - if (shortcut.isCmd) parts.push('Cmd') if (shortcut.isAlt) parts.push('Alt') if (shortcut.isCtrl) parts.push('Ctrl') if (shortcut.isShift) parts.push('Shift') + if (shortcut.isCmd) parts.push('CommandOrControl') parts.push(shortcut.natural.toUpperCase()) diff --git a/src/shortcuts.rs b/src/shortcuts.rs index a069080..ae867c0 100644 --- a/src/shortcuts.rs +++ b/src/shortcuts.rs @@ -2,16 +2,8 @@ use tauri::{AppHandle, GlobalShortcutManager, Manager, Window}; use crate::window; -// todo: maybe use `cfg-if` crate for many platform-specific stuff -#[cfg(target_os = "macos")] -pub const DEFAULT_SHORTCUT: &str = "Cmd+Alt+Shift+C"; -#[cfg(target_os = "macos")] -pub const SETTINGS_SHORTCUT: &str = "Cmd+,"; - -#[cfg(target_os = "windows")] -pub const DEFAULT_SHORTCUT: &str = "Ctrl+Alt+Shift+C"; -#[cfg(target_os = "windows")] -pub const SETTINGS_SHORTCUT: &str = "Ctrl+Shift+,"; +pub const DEFAULT_SHORTCUT: &str = "CommandOrControl+Alt+Shift+C"; +pub const SETTINGS_SHORTCUT: &str = "CommandOrControl+,"; pub fn update_default( app: &AppHandle, From 366bb648f06bafa021a9cde0fd758f2c91ffdf55 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Wed, 16 Aug 2023 12:22:17 +0800 Subject: [PATCH 7/7] Fix tray menu accelerator display --- src/tray.rs | 4 ++-- src/window/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tray.rs b/src/tray.rs index 7e278d9..bade025 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -15,13 +15,13 @@ pub enum TrayMenu { pub fn build() -> SystemTray { let tray_menu = SystemTrayMenu::new() - .add_item(CustomMenuItem::new(TrayMenu::Settings, "Settings...").accelerator("Cmd+,")) + .add_item(CustomMenuItem::new(TrayMenu::Settings, "Settings...").accelerator("CommandOrControl+,")) .add_native_item(SystemTrayMenuItem::Separator); #[cfg(debug_assertions)] let tray_menu = tray_menu .add_item( - CustomMenuItem::new(TrayMenu::DevTools, "Open DevTools").accelerator("Cmd+Shift+I"), + CustomMenuItem::new(TrayMenu::DevTools, "Open DevTools").accelerator("CommandOrControl+Shift+I"), ) .add_native_item(SystemTrayMenuItem::Separator); diff --git a/src/window/mod.rs b/src/window/mod.rs index fcf8947..0d66e3c 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -7,7 +7,7 @@ use tauri_plugin_spotlight::{PluginConfig, WindowConfig}; use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial, NSVisualEffectState}; #[cfg(target_os = "windows")] -use window_vibrancy::apply_blur; +use window_vibrancy::apply_mica; use crate::shortcuts; @@ -83,6 +83,6 @@ impl TransparentWindow for Window { // not working #[cfg(target_os = "windows")] - apply_blur(self, Some((18, 18, 18, 125))) + apply_mica(self, Some(false)) } }