Skip to content

Commit 1c0335f

Browse files
authored
fix: fix the focusing problem of the input box in windows (#481)
1 parent 8498578 commit 1c0335f

4 files changed

Lines changed: 112 additions & 4 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2"
8181
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\"))".dependencies]
8282
tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] }
8383

84-
8584
[profile.dev]
8685
incremental = true # Compile your binary in smaller steps.
8786

@@ -96,3 +95,6 @@ strip = true # Ensures debug symbols are removed.
9695
tauri-plugin-autostart = "^2.2"
9796
tauri-plugin-global-shortcut = "2"
9897
tauri-plugin-updater = { git = "https://github.com/infinilabs/plugins-workspace", branch = "v2" }
98+
99+
[target."cfg(target_os = \"windows\")".dependencies]
100+
enigo="0.3"

src-tauri/src/lib.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ pub fn run() {
137137
local::application::get_default_search_paths,
138138
local::application::list_app_with_metadata_in,
139139
util::open,
140-
server::system_settings::get_system_settings
140+
server::system_settings::get_system_settings,
141+
simulate_mouse_click
141142
])
142143
.setup(|app| {
143144
let registry = SearchSourceRegistry::default();
@@ -251,13 +252,13 @@ async fn init_app_search_source<R: Runtime>(app_handle: &AppHandle<R>) -> Result
251252
#[tauri::command]
252253
async fn show_coco<R: Runtime>(app_handle: AppHandle<R>) {
253254
if let Some(window) = app_handle.get_window(MAIN_WINDOW_LABEL) {
254-
let _ = app_handle.emit("show-coco", ());
255-
256255
move_window_to_active_monitor(&window);
257256

258257
let _ = window.show();
259258
let _ = window.unminimize();
260259
let _ = window.set_focus();
260+
261+
let _ = app_handle.emit("show-coco", ());
261262
}
262263
}
263264

@@ -412,3 +413,49 @@ async fn get_app_search_source<R: Runtime>(app_handle: AppHandle<R>) -> Result<(
412413
async fn show_settings(app_handle: AppHandle) {
413414
open_settings(&app_handle);
414415
}
416+
417+
#[tauri::command]
418+
async fn simulate_mouse_click<R: Runtime>(window: WebviewWindow<R>, is_chat_mode: bool) {
419+
#[cfg(target_os = "windows")]
420+
{
421+
use enigo::{Button, Coordinate, Direction, Enigo, Mouse, Settings};
422+
use std::{thread, time::Duration};
423+
424+
if let Ok(mut enigo) = Enigo::new(&Settings::default()) {
425+
// Save the current mouse position
426+
if let Ok((original_x, original_y)) = enigo.location() {
427+
// Retrieve the window's outer position (top-left corner)
428+
if let Ok(position) = window.outer_position() {
429+
// Retrieve the window's inner size (client area)
430+
if let Ok(size) = window.inner_size() {
431+
// Calculate the center position of the title bar
432+
let x = position.x + (size.width as i32 / 2);
433+
let y = if is_chat_mode {
434+
position.y + size.height as i32 - 50
435+
} else {
436+
position.y + 30
437+
};
438+
439+
// Move the mouse cursor to the calculated position
440+
if enigo.move_mouse(x, y, Coordinate::Abs).is_ok() {
441+
// // Simulate a left mouse click
442+
let _ = enigo.button(Button::Left, Direction::Click);
443+
// let _ = enigo.button(Button::Left, Direction::Release);
444+
445+
thread::sleep(Duration::from_millis(100));
446+
447+
// Move the mouse cursor back to the original position
448+
let _ = enigo.move_mouse(original_x, original_y, Coordinate::Abs);
449+
}
450+
}
451+
}
452+
}
453+
}
454+
}
455+
456+
#[cfg(not(target_os = "windows"))]
457+
{
458+
let _ = window;
459+
let _ = is_chat_mode;
460+
}
461+
}

src/components/SearchChat/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,23 @@ function SearchChat({
9090

9191
const setTheme = useThemeStore((state) => state.setTheme);
9292

93+
const isChatModeRef = useRef(false);
94+
useEffect(() => {
95+
isChatModeRef.current = isChatMode;
96+
}, [isChatMode]);
97+
9398
useMount(async () => {
9499
const isWin10 = await platformAdapter.isWindows10();
95100

96101
setIsWin10(isWin10);
102+
103+
platformAdapter.listenEvent("show-coco", () => {
104+
console.log("show-coco");
105+
106+
platformAdapter.invokeBackend("simulate_mouse_click", {
107+
isChatMode: isChatModeRef.current,
108+
});
109+
});
97110
});
98111

99112
useEffect(() => {

0 commit comments

Comments
 (0)