Skip to content

Commit

Permalink
feat: Implement a proper show/hide UI function (#99)
Browse files Browse the repository at this point in the history
* feat: Implement a proper show/hide UI function

Hide the app icon from the dock bar, and add a new menu item to show UI again after hide.

Fixes #85

* Make the UI window always on top
  • Loading branch information
huytd authored Oct 6, 2023
1 parent 9f727c3 commit f96a50a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 37 deletions.
9 changes: 3 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ version = "0.1.2"
env_logger = "0.10.0"
libc = "0.2.139"
log = "0.4.17"
vi = { git = "https:/github.com/zerox-dg/vi-rs", branch = "master" }
vi = { git = "https://github.com/zerox-dg/vi-rs", branch = "master" }
bitflags = "1.3.2"
druid = { version = "0.8.2", features = ["image", "png"] }
druid = { features = ["image", "png"], git = "https://github.com/huytd/druid", branch = "master" }
once_cell = "1.17.0"
auto-launch = "0.5.0"

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ fn main() {
.title("gõkey")
.window_size((ui::WINDOW_WIDTH, ui::WINDOW_HEIGHT))
.set_position(ui::center_window_position())
.set_always_on_top(true)
.resizable(false);
let app = AppLauncher::with_window(win);
let event_sink = app.get_external_handle();
Expand Down
3 changes: 0 additions & 3 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ pub const SYMBOL_CTRL: &str = "⌃";
pub const SYMBOL_SUPER: &str = "❖";
pub const SYMBOL_ALT: &str = "⌥";

// TODO: support HIDE_WINDOW on next druid future versions
pub const HIDE_COMMAND: Selector = CLOSE_WINDOW;

pub fn get_home_dir() -> Option<PathBuf> {
env::var("HOME").ok().map(PathBuf::from)
}
Expand Down
2 changes: 0 additions & 2 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use core_graphics::{
},
sys,
};
use druid::{commands::HIDE_APPLICATION, Selector};
use objc::{class, msg_send, sel, sel_impl};

pub use macos_ext::SystemTray;
Expand Down Expand Up @@ -46,7 +45,6 @@ pub const SYMBOL_CTRL: &str = "⌃";
pub const SYMBOL_SUPER: &str = "⌘";
pub const SYMBOL_ALT: &str = "⌥";

pub const HIDE_COMMAND: Selector = HIDE_APPLICATION;
static AUTO_LAUNCH: Lazy<AutoLaunch> = Lazy::new(|| {
let app_path = get_current_app_path();
let app_name = Path::new(&app_path)
Expand Down
16 changes: 9 additions & 7 deletions src/platform/macos_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use cocoa::appkit::{
NSApp, NSApplication, NSButton, NSMenu, NSMenuItem, NSStatusBar, NSStatusItem,
};
use cocoa::appkit::{NSApp, NSApplication, NSButton, NSMenu, NSMenuItem, NSStatusBar, NSStatusItem};
use cocoa::base::{nil, YES};
use cocoa::foundation::{NSAutoreleasePool, NSString};
use core_foundation::dictionary::CFDictionaryRef;
Expand Down Expand Up @@ -31,6 +29,7 @@ impl Data for Wrapper {
}

pub enum SystemTrayMenuItemKey {
ShowUI,
Enable,
TypingMethodTelex,
TypingMethodVNI,
Expand Down Expand Up @@ -75,6 +74,8 @@ impl SystemTray {
}

pub fn init_menu_items(&self) {
self.add_menu_item("Bật bảng điều khiển", || ());
self.add_menu_separator();
self.add_menu_item("Tắt gõ tiếng việt", || ());
self.add_menu_separator();
self.add_menu_item("Telex ✓", || ());
Expand Down Expand Up @@ -109,10 +110,11 @@ impl SystemTray {

pub fn get_menu_item_index_by_key(&self, key: SystemTrayMenuItemKey) -> i64 {
match key {
SystemTrayMenuItemKey::Enable => 0,
SystemTrayMenuItemKey::TypingMethodTelex => 2,
SystemTrayMenuItemKey::TypingMethodVNI => 3,
SystemTrayMenuItemKey::Exit => 5,
SystemTrayMenuItemKey::ShowUI => 0,
SystemTrayMenuItemKey::Enable => 2,
SystemTrayMenuItemKey::TypingMethodTelex => 4,
SystemTrayMenuItemKey::TypingMethodVNI => 5,
SystemTrayMenuItemKey::Exit => 7,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bitflags::bitflags;
pub use os::{
ensure_accessibility_permission, get_active_app_name, get_home_dir, is_in_text_selection,
is_launch_on_login, run_event_listener, send_backspace, send_string, update_launch_on_login,
Handle, HIDE_COMMAND, SYMBOL_ALT, SYMBOL_CTRL, SYMBOL_SHIFT, SYMBOL_SUPER,
Handle, SYMBOL_ALT, SYMBOL_CTRL, SYMBOL_SHIFT, SYMBOL_SUPER,
};

#[cfg(target_os = "macos")]
Expand Down
3 changes: 0 additions & 3 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ pub const SYMBOL_CTRL: &str = "⌃";
pub const SYMBOL_SUPER: &str = "⊞";
pub const SYMBOL_ALT: &str = "⌥";

// TODO: support HIDE_WINDOW on next druid future versions
pub const HIDE_COMMAND: Selector = CLOSE_WINDOW;

pub fn get_home_dir() -> Option<PathBuf> {
env::var("USERPROFILE").ok().map(PathBuf::from)
.or_else(|| env::var("HOMEDRIVE").ok().and_then(|home_drive| {
Expand Down
33 changes: 20 additions & 13 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use crate::{
input::{rebuild_keyboard_layout_map, TypingMethod, INPUT_STATE},
platform::{
self, is_launch_on_login, update_launch_on_login, KeyModifier, SystemTray,
is_launch_on_login, update_launch_on_login, KeyModifier, SystemTray,
SystemTrayMenuItemKey, SYMBOL_ALT, SYMBOL_CTRL, SYMBOL_SHIFT, SYMBOL_SUPER,
},
UI_EVENT_SINK,
};
use druid::{
commands::QUIT_APP,
theme::{BACKGROUND_DARK, BORDER_DARK, PLACEHOLDER_COLOR},
widget::{
Button, Checkbox, Container, Controller, FillStrat, Flex, Image, Label, LineBreaking,
RadioGroup, Switch, TextBox,
},
Application, Data, Env, Event, EventCtx, ImageBuf, Lens, Screen, Selector, Target, Widget,
WidgetExt,
};
use druid::{commands::{QUIT_APP}, theme::{BACKGROUND_DARK, BORDER_DARK, PLACEHOLDER_COLOR}, widget::{
Button, Checkbox, Container, Controller, FillStrat, Flex, Image, Label, LineBreaking,
RadioGroup, Switch, TextBox,
}, Application, Data, Env, Event, EventCtx, ImageBuf, Lens, Screen, Selector, Target, Widget, WidgetExt};
use log::error;

pub const UPDATE_UI: Selector = Selector::new("gox-ui.update-ui");
pub const SHOW_UI: Selector = Selector::new("gox-ui.show-ui");
pub const WINDOW_WIDTH: f64 = 320.0;
pub const WINDOW_HEIGHT: f64 = 268.0;

Expand Down Expand Up @@ -142,6 +137,12 @@ impl UIDataAdapter {
}

fn setup_system_tray_actions(&mut self) {
self.systray
.set_menu_item_callback(SystemTrayMenuItemKey::ShowUI, || {
UI_EVENT_SINK
.get()
.map(|event| Some(event.submit_command(SHOW_UI, (), Target::Auto)));
});
self.systray
.set_menu_item_callback(SystemTrayMenuItemKey::Enable, || {
unsafe {
Expand Down Expand Up @@ -202,10 +203,14 @@ impl<W: Widget<UIDataAdapter>> Controller<UIDataAdapter, W> for UIController {
data.update();
rebuild_keyboard_layout_map();
}
if cmd.get(SHOW_UI).is_some() {
ctx.set_handled();
ctx.window().bring_to_front_and_focus();
}
}
Event::WindowCloseRequested => {
ctx.set_handled();
ctx.submit_command(platform::HIDE_COMMAND);
ctx.window().hide();
}
_ => {}
}
Expand Down Expand Up @@ -353,7 +358,9 @@ pub fn main_ui_builder() -> impl Widget<UIDataAdapter> {
Button::new("Đóng")
.fix_width(100.0)
.fix_height(28.0)
.on_click(|event, _, _| event.submit_command(platform::HIDE_COMMAND)),
.on_click(|event, _, _| {
event.window().hide();
}),
)
.cross_axis_alignment(druid::widget::CrossAxisAlignment::Start)
.main_axis_alignment(druid::widget::MainAxisAlignment::End)
Expand Down

0 comments on commit f96a50a

Please sign in to comment.