Skip to content

Commit

Permalink
fix windows bug
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Dec 26, 2024
1 parent 9e1509a commit 807db72
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 57 deletions.
76 changes: 29 additions & 47 deletions src-tauri/src/service/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ use tauri_plugin_positioner::{Position, WindowExt};

/// App
pub fn init_window() {
tauri::async_runtime::spawn(async {
let size = calculate_logical_size(MAIN_WINDOW_X, MAIN_WINDOW_Y).await;
#[cfg(any(windows, target_os = "macos"))]
{
get_main_window()
.set_size(size)
.expect("Failed to set window size");

#[cfg(any(windows, target_os = "macos"))]
{
let _ = get_main_window().set_decorations(false);
let _ = get_main_window().set_shadow(true);
}
.set_decorations(false)
.expect("Failed to set decorations");
get_main_window()
.set_shadow(false)
.expect("Failed to set shadow");
}

#[cfg(debug_assertions)]
{
get_main_window().open_devtools();
}
});
#[cfg(debug_assertions)]
{
get_main_window().open_devtools();
}
}

pub fn toggle_main_window() {
Expand Down Expand Up @@ -72,7 +69,6 @@ pub fn toggle_main_window() {
)
.expect("Failed to emit change tab event");
get_main_window().show().expect("Failed to show window");

register_hotkeys(true);
get_main_window()
.emit(
Expand Down Expand Up @@ -134,27 +130,9 @@ pub fn position_window_near_cursor() {

if let Ok(cursor_position) = window.cursor_position() {
let window_size = window.outer_size().expect("Failed to get window size");

// Get current monitor or fallback to primary
let current_monitor = window
.available_monitors()
.expect("Failed to get available monitors")
.into_iter()
.find(|monitor| {
let pos = monitor.position();
let size = monitor.size();
let bounds = (
pos.x as f64,
pos.y as f64,
pos.x as f64 + size.width as f64,
pos.y as f64 + size.height as f64,
);

cursor_position.x >= bounds.0
&& cursor_position.x < bounds.2
&& cursor_position.y >= bounds.1
&& cursor_position.y < bounds.3
})
.current_monitor()
.expect("Failed to get current monitor")
.unwrap_or_else(|| {
window
.primary_monitor()
Expand All @@ -166,37 +144,41 @@ pub fn position_window_near_cursor() {
let monitor_pos = current_monitor.position();
let monitor_size = current_monitor.size();

// Calculate window position with offset
// Account for Windows DPI scaling
#[cfg(windows)]
let (cursor_x, cursor_y) = (
cursor_position.x / scale_factor,
cursor_position.y / scale_factor,
);
#[cfg(not(windows))]
let (cursor_x, cursor_y) = (cursor_position.x, cursor_position.y);

let pos = PhysicalPosition::new(
((cursor_position.x + 10.0) * scale_factor) as i32,
((cursor_position.y + 10.0) * scale_factor) as i32,
(cursor_x * scale_factor) as i32,
(cursor_y * scale_factor) as i32,
);

// Calculate monitor bounds in physical pixels
let monitor_bounds = (
(monitor_pos.x as f64 * scale_factor) as i32,
(monitor_pos.y as f64 * scale_factor) as i32,
(monitor_pos.x as f64 * scale_factor + monitor_size.width as f64 * scale_factor) as i32,
(monitor_pos.y as f64 * scale_factor + monitor_size.height as f64 * scale_factor)
as i32,
((monitor_pos.x as f64 + monitor_size.width as f64) * scale_factor) as i32,
((monitor_pos.y as f64 + monitor_size.height as f64) * scale_factor) as i32,
);

// Constrain window position within monitor bounds
let final_pos = PhysicalPosition::new(
pos.x
.max(monitor_bounds.0)
.min(monitor_bounds.2 - window_size.width as i32),
.min(monitor_bounds.2 - (window_size.width as f64 * scale_factor) as i32),
pos.y
.max(monitor_bounds.1)
.min(monitor_bounds.3 - window_size.height as i32),
.min(monitor_bounds.3 - (window_size.height as f64 * scale_factor) as i32),
);

window
.set_position(final_pos)
.expect("Failed to set window position");
}
}

pub fn calculate_thumbnail_dimensions(width: u32, height: u32) -> (u32, u32) {
let aspect_ratio = width as f64 / height as f64;
if width > MAX_IMAGE_DIMENSIONS || height > MAX_IMAGE_DIMENSIONS {
Expand Down
4 changes: 0 additions & 4 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"csp": null
},
"macOSPrivateApi": true,
"trayIcon": {
"iconPath": "icons/32x32.png",
"iconAsTemplate": true
},
"windows": [
{
"fullscreen": false,
Expand Down
8 changes: 4 additions & 4 deletions src/components/pages/app/clipboard/base-clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ export const BaseClipboard: Component<BaseClipboardProps> = (props) => {
title="Star"
class={`${
clipboard.star ? "text-yellow-400 dark:text-yellow-300" : "hidden text-zinc-700"
} cursor-pointer text-lg hover:text-yellow-400 group-hover:block dark:text-white dark:hover:text-yellow-300`}
} cursor-pointer hover:text-yellow-400 group-hover:block dark:text-white dark:hover:text-yellow-300`}
/>
<div class="flex items-center gap-1">
{props.data.rtf && (
<BsJournalRichtext
onClick={handleRtfCopy}
title="Copy as RTF"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-blue-600 group-hover:block dark:text-white dark:hover:text-blue-400"
class="hidden cursor-pointer text-zinc-700 hover:text-blue-600 group-hover:block dark:text-white dark:hover:text-blue-400"
/>
)}
{props.data.html && (
<TbSourceCode
onClick={handleHtmlCopy}
title="Copy as HTML"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-green-600 group-hover:block dark:text-white dark:hover:text-green-400"
class="hidden cursor-pointer text-zinc-700 hover:text-green-600 group-hover:block dark:text-white dark:hover:text-green-400"
/>
)}
</div>
Expand All @@ -103,7 +103,7 @@ export const BaseClipboard: Component<BaseClipboardProps> = (props) => {
handleDelete(clipboard.id);
}}
title="Delete"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-red-600 group-hover:block dark:text-white dark:hover:text-red-600"
class="hidden cursor-pointer text-zinc-700 hover:text-red-600 group-hover:block dark:text-white dark:hover:text-red-600"
/>
</div>

Expand Down
5 changes: 3 additions & 2 deletions src/components/pages/app/clipboard/text-clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface TextClipboardProps {

export const TextClipboard: Component<TextClipboardProps> = (props) => {
let type = ClipboardType.Text;

let data = props.data.text?.data;
let textType = props.data.text?.type as ClipboardTextType;

Expand Down Expand Up @@ -68,7 +69,7 @@ export const TextClipboard: Component<TextClipboardProps> = (props) => {
e.stopPropagation();
await invokeCommand(InvokeCommand.CopyClipboard, {
id: props.data.clipboard.id,
type,
type: ClipboardType.Text,
});
};

Expand All @@ -79,7 +80,7 @@ export const TextClipboard: Component<TextClipboardProps> = (props) => {
<ClipboardHeader {...props} Icon={getIcon()} />

<div class="min-w-0 flex-1">
<p class="w-[calc(100vw-6.5rem)] truncate text-left text-sm">{data}</p>
<p class="w-[calc(100vw-6.576rem)] truncate text-left text-sm">{data}</p>
<div
class="text-left text-xs font-thin text-zinc-700 dark:text-zinc-300"
title={new Date(props.data.clipboard.created_date).toLocaleString()}
Expand Down

0 comments on commit 807db72

Please sign in to comment.