Skip to content

Commit

Permalink
feat: support edit btn click move preview window
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaYa committed Nov 26, 2024
1 parent 6abae99 commit bb7c5a1
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@tauri-apps/plugin-fs": "~2.0.2",
"@tauri-apps/plugin-global-shortcut": "~2",
"@tauri-apps/plugin-os": "~2",
"@tauri-apps/plugin-positioner": "~2",
"@tauri-apps/plugin-shell": "^2.0.1",
"@tauri-apps/plugin-store": "~2",
"@vueuse/core": "^11.2.0",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

16 changes: 16 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ custom-protocol = ["tauri/custom-protocol"]
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-autostart = "2"
tauri-plugin-global-shortcut = "2"
tauri-plugin-positioner = { version = "2.0.0", features = ["tray-icon"] }

[target."cfg(target_os = \"macos\")".dependencies]
cocoa = "0.25"
4 changes: 3 additions & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"core:event:allow-emit",
"core:event:allow-emit-to",
"core:event:allow-listen",
"core:event:allow-unlisten"
"core:event:allow-unlisten",
"positioner:allow-move-window",
"positioner:allow-set-tray-icon-state"
]
}
2 changes: 1 addition & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod window;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_positioner::init())
.plugin(tauri_plugin_os::init())
.setup(|app| {
#[cfg(desktop)]
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub fn get_app_menu(app: &AppHandle) -> Result<Menu<tauri::Wry>, tauri::Error> {
}

fn handle_tray_icon_events(_tray: &TrayIcon, event: TrayIconEvent) {
tauri_plugin_positioner::on_tray_event(_tray.app_handle(), &event);
if let TrayIconEvent::DoubleClick { .. } = event {
info!("Double click");
}
Expand Down
16 changes: 12 additions & 4 deletions src-tauri/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{thread::sleep, time::Duration};

use crate::window;
use tauri::{PhysicalSize, WebviewWindow};
use tauri_plugin_positioner::{Position, WindowExt};

pub fn show_preview_window(window: &WebviewWindow) {
let _ = window.show();
Expand All @@ -10,22 +13,27 @@ pub fn show_preview_window(window: &WebviewWindow) {
}

pub fn update_preview_window(window: &WebviewWindow) {
if (!window.is_visible().unwrap_or_default()) {
if !window.is_visible().unwrap_or_default() {
show_preview_window(window);
}
let _ = window.unminimize();
window::center_position(window);
let _ = window.set_decorations(true);
let _ = window.set_focus();
let _ = window.set_resizable(true);
let _ = window.set_always_on_top(false);
if let Some(monitor) = window::find_monitor(window) {
let screen_size = monitor.size();
let size = PhysicalSize {
width: screen_size.width - 360,
height: screen_size.height - 860,
width: screen_size.width / 2,
height: screen_size.height / 2,
};
let _ = window.set_size(tauri::Size::Physical(size));
// sleep 0.3
let window = window.clone();
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(100));
let _ = window.move_window(Position::Center);
});
}
}

Expand Down
32 changes: 16 additions & 16 deletions src-tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ pub fn get_main_window(app: &AppHandle) -> WebviewWindow {
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
0.5,
33.0 / 255.0,
54.0 / 255.0,
201.0 / 255.0,
0.1,
);
ns_window.setBackgroundColor_(bg_color);
}
Expand Down Expand Up @@ -131,10 +131,10 @@ pub fn get_setting_window(app: &AppHandle) -> WebviewWindow {
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
0.5,
33.0 / 255.0,
54.0 / 255.0,
201.0 / 255.0,
0.1,
);
ns_window.setBackgroundColor_(bg_color);
}
Expand Down Expand Up @@ -171,10 +171,10 @@ pub fn get_preview_window(app: &AppHandle) -> WebviewWindow {

let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
0.5,
33.0 / 255.0,
54.0 / 255.0,
201.0 / 255.0,
0.0,
);
ns_window.setBackgroundColor_(bg_color);
}
Expand Down Expand Up @@ -211,10 +211,10 @@ pub fn get_startup_window(app: &AppHandle) -> WebviewWindow {
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
50.0 / 255.0,
158.0 / 255.0,
163.5 / 255.0,
0.5,
33.0 / 255.0,
54.0 / 255.0,
201.0 / 255.0,
0.1,
);
ns_window.setBackgroundColor_(bg_color);
}
Expand Down

0 comments on commit bb7c5a1

Please sign in to comment.