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

Fix focus issue when opening Qopy #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ on:
branches:
- main
workflow_dispatch:
inputs:
ref:
description: 'Branch or tag to build e.g. main or v0.1.0'
required: true
type: string
default: 'main'

jobs:
prepare:
Expand All @@ -16,6 +22,8 @@ jobs:
version: ${{ steps.get_version.outputs.VERSION }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.ref }}
- name: Get version
id: get_version
run: echo "VERSION=$(node -p "require('./src-tauri/tauri.conf.json').version")" >> $GITHUB_OUTPUT
Expand All @@ -31,6 +39,8 @@ jobs:
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.ref }}

- name: setup node
uses: actions/setup-node@v4
Expand Down
26 changes: 25 additions & 1 deletion app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,36 @@ onMounted(async () => {
await navigateTo('/keybind')
await app.show();
await window.getCurrentWindow().show();
manageFocus();
})

await listen('main_route', async () => {
await navigateTo('/')
})
})

function manageFocus() {
if (process.platform === 'win32') {
const { SetForegroundWindow, AttachThreadInput, GetForegroundWindow, GetWindowThreadProcessId } = require('windows-api');
const foregroundWindow = GetForegroundWindow();
const currentThreadId = GetWindowThreadProcessId(foregroundWindow, null);
const targetThreadId = GetWindowThreadProcessId(window.hwnd(), null);

AttachThreadInput(currentThreadId, targetThreadId, 1);
SetForegroundWindow(window.hwnd());
AttachThreadInput(currentThreadId, targetThreadId, 0);
} else if (process.platform === 'darwin') {
const { NSWindow } = require('cocoa');
const nsWindow = window.ns_window();
nsWindow.makeKeyAndOrderFront(true);
} else if (process.platform === 'linux') {
const { XOpenDisplay, XDefaultRootWindow, XSetInputFocus, XCloseDisplay, RevertToParent } = require('xlib');
const display = XOpenDisplay(null);
const rootWindow = XDefaultRootWindow(display);
XSetInputFocus(display, rootWindow, RevertToParent, 0);
XCloseDisplay(display);
}
}
</script>

<style lang="scss">
Expand Down Expand Up @@ -72,4 +96,4 @@ body,
.os-scrollbar-horizontal {
display: none;
}
</style>
</style>
50 changes: 48 additions & 2 deletions src-tauri/src/api/hotkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ use crate::utils::commands::center_window_on_current_monitor;
use rdev::{listen, EventType, Key};
use tauri::Manager;

#[cfg(target_os = "windows")]
mod platform {
use std::ptr::null_mut;
use winapi::um::winuser::{AttachThreadInput, GetForegroundWindow, GetWindowThreadProcessId, SetForegroundWindow};

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let foreground_window = GetForegroundWindow();
let current_thread_id = GetWindowThreadProcessId(foreground_window, null_mut());
let target_thread_id = GetWindowThreadProcessId(window.hwnd() as _, null_mut());

AttachThreadInput(current_thread_id, target_thread_id, 1);
SetForegroundWindow(window.hwnd() as _);
AttachThreadInput(current_thread_id, target_thread_id, 0);
}
}
}

#[cfg(target_os = "macos")]
mod platform {
use cocoa::appkit::NSWindow;
use cocoa::base::id;
use objc::runtime::YES;

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let ns_window: id = window.ns_window().unwrap() as _;
ns_window.makeKeyAndOrderFront_(YES);
}
}
}

#[cfg(target_os = "linux")]
mod platform {
use x11::xlib::{Display, XSetInputFocus, XDefaultRootWindow, XOpenDisplay, XCloseDisplay, RevertToParent};

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let display: *mut Display = XOpenDisplay(null_mut());
let root_window = XDefaultRootWindow(display);
XSetInputFocus(display, root_window, RevertToParent, 0);
XCloseDisplay(display);
}
}
}

fn key_to_string(key: &Key) -> String {
format!("{:?}", key)
}
Expand Down Expand Up @@ -35,10 +81,10 @@ pub fn setup(app_handle: tauri::AppHandle) {
pressed_keys.iter_mut().for_each(|k| *k = false);
let window = app_handle.get_webview_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
center_window_on_current_monitor(&window);
platform::manage_focus(&window);
}
})
.unwrap();
});
}
}
48 changes: 47 additions & 1 deletion src-tauri/src/api/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@ use tauri::{
menu::{MenuBuilder, MenuItemBuilder}, tray::TrayIconBuilder, Emitter, Manager
};

#[cfg(target_os = "windows")]
mod platform {
use std::ptr::null_mut;
use winapi::um::winuser::{AttachThreadInput, GetForegroundWindow, GetWindowThreadProcessId, SetForegroundWindow};

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let foreground_window = GetForegroundWindow();
let current_thread_id = GetWindowThreadProcessId(foreground_window, null_mut());
let target_thread_id = GetWindowThreadProcessId(window.hwnd() as _, null_mut());

AttachThreadInput(current_thread_id, target_thread_id, 1);
SetForegroundWindow(window.hwnd() as _);
AttachThreadInput(current_thread_id, target_thread_id, 0);
}
}
}

#[cfg(target_os = "macos")]
mod platform {
use cocoa::appkit::NSWindow;
use cocoa::base::id;
use objc::runtime::YES;

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let ns_window: id = window.ns_window().unwrap() as _;
ns_window.makeKeyAndOrderFront_(YES);
}
}
}

#[cfg(target_os = "linux")]
mod platform {
use x11::xlib::{Display, XSetInputFocus, XDefaultRootWindow, XOpenDisplay, XCloseDisplay, RevertToParent};

pub fn manage_focus(window: &tauri::Window) {
unsafe {
let display: *mut Display = XOpenDisplay(null_mut());
let root_window = XDefaultRootWindow(display);
XSetInputFocus(display, root_window, RevertToParent, 0);
XCloseDisplay(display);
}
}
}

pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
let window = app.get_webview_window("main").unwrap();
let window_clone_for_tray = window.clone();
Expand Down Expand Up @@ -30,7 +76,7 @@ pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
window_clone_for_tray.hide().unwrap();
} else {
window_clone_for_tray.show().unwrap();
window_clone_for_tray.set_focus().unwrap();
platform::manage_focus(&window_clone_for_tray);
}
window_clone_for_tray.emit("main_route", ()).unwrap();
}
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ fn main() {
.on_window_event(|app, event| {
#[cfg(not(dev))]
if let tauri::WindowEvent::Focused(false) = event {
if let Some(window) = app.get_webview_window("main") {
let _ = window.hide();
}
return;
}
})
.invoke_handler(tauri::generate_handler![
Expand Down