Skip to content

Commit

Permalink
update: pin_win use phx instead of px
Browse files Browse the repository at this point in the history
  • Loading branch information
Horbin-Magician committed Jan 4, 2025
1 parent 9f83227 commit a5933f0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 117 deletions.
32 changes: 14 additions & 18 deletions src/module/screen_shotter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl ScreenShotter{
);
mask_win.set_bac_image(slint::Image::from_rgba8(bac_buffer.clone()));
mask_win.set_detected(false);
mask_win.set_select_rect( crate::ui::Rect{ x: -1.0, y: -1.0, width: 0.0, height: 0.0 });
mask_win.set_select_rect( crate::ui::Rect{ x: -1, y: -1, width: 0, height: 0 });

// refresh window
let scale_factor = sys_util::get_scale_factor(monitor.id());
Expand Down Expand Up @@ -206,10 +206,10 @@ impl ScreenShotter{
if *x <= mouse_x_phs && mouse_x_phs <= *x + width && *y <= mouse_y_phs && mouse_y_phs <= *y + height {
mask_win.set_select_rect(
crate::ui::Rect {
x: *x as f32 + 1.0,
y: *y as f32 + 1.0,
width: *width as f32 - 1.0,
height: *height as f32 - 1.0
x: *x as i32 + 1,
y: *y as i32 + 1,
width: *width as i32 - 1,
height: *height as i32 - 1
}
);
if_set = true;
Expand All @@ -218,7 +218,7 @@ impl ScreenShotter{
}
if if_set == false {
mask_win.set_select_rect(
crate::ui::Rect{ x: window_x as f32, y: window_y as f32, width: window_width as f32, height: window_height as f32 }
crate::ui::Rect{ x: window_x, y: window_y, width: window_width, height: window_height }
);
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ impl ScreenShotter{
let message_sender_clone = message_sender.clone();
let bac_buffer_rc_clone = bac_buffer_rc.clone();
mask_win.on_new_pin_win(move |rect| {
if (rect.width * rect.height) < 1. { return; } // ignore too small rect
if (rect.width * rect.height) < 1 { return; } // ignore too small rect
if let Some(mask_win) = mask_win_clone.upgrade() {
let mut max_pin_win_id = max_pin_win_id_clone.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
Expand All @@ -265,7 +265,7 @@ impl ScreenShotter{

if let Ok(pin_win) = PinWin::new(
img, rect,
mask_win.get_offset_x(), mask_win.get_offset_y(), mask_win.get_scale_factor(),
mask_win.get_offset_x(), mask_win.get_offset_y(),
*max_pin_win_id, message_sender_clone
) {
let pin_window_clone = pin_win.pin_window.as_weak();
Expand Down Expand Up @@ -401,11 +401,10 @@ impl ScreenShotter{

fn update_pin_win_record(id:u32, pin_win: &PinWindow) {
let position = pin_win.window().position();
let scale_factor = pin_win.get_scale_factor();
let rect_x = pin_win.get_img_x() * scale_factor;
let rect_y = pin_win.get_img_y() * scale_factor;
let rect_width = pin_win.get_img_width() * scale_factor;
let rect_height = pin_win.get_img_height() * scale_factor;
let rect_x = pin_win.get_img_x();
let rect_y = pin_win.get_img_y();
let rect_width = pin_win.get_img_width();
let rect_height = pin_win.get_img_height();
ShotterRecord::global().lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.update_shotter(id, shotter_record::ShotterConfig{
Expand Down Expand Up @@ -443,25 +442,22 @@ impl ScreenShotter{

let mut pos_x = 0;
let mut pos_y = 0;
let mut scale_factor = 1.0;
if let Ok(m) = Monitor::from_point(shotter.pos_x, shotter.pos_y) {
if let Ok(_) = Monitor::from_point(shotter.pos_x, shotter.pos_y) {
pos_x = shotter.pos_x;
pos_y = shotter.pos_y;
scale_factor = sys_util::get_scale_factor(m.id());
} else {
for m in Monitor::all().unwrap() {
if m.is_primary() {
pos_x = m.x();
pos_y = m.y();
scale_factor = sys_util::get_scale_factor(m.id());
}
}
}

let offset_x = pos_x - rect.x as i32;
let offset_y = pos_y - rect.y as i32;
let pin_win = PinWin::new(
img_buffer, rect, offset_x, offset_y, scale_factor, id, message_sender_clone
img_buffer, rect, offset_x, offset_y, id, message_sender_clone
)?;
pin_win.pin_window.set_zoom_factor(shotter.zoom_factor);

Expand Down
71 changes: 30 additions & 41 deletions src/module/screen_shotter/pin_win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl PinWin {
rect: Rect,
offset_x: i32,
offset_y: i32,
true_scale_factor: f32,
id: u32,
message_sender: Sender<ShotterMessage>,
) -> Result<PinWin, Box<dyn Error>> {
Expand All @@ -32,17 +31,15 @@ impl PinWin {

{ // set bash properties
let border_width = pin_window.get_win_border_width();
let scale_factor = pin_window.window().scale_factor();

pin_window.set_scale_factor(scale_factor);

pin_window.window().set_position(slint::LogicalPosition::new((rect.x + offset_x as f32) / scale_factor - border_width, (rect.y + offset_y as f32) / scale_factor - border_width));
// let scale_factor = pin_window.window().scale_factor();
// pin_window.set_scale_factor(scale_factor);
pin_window.window().set_position(slint::PhysicalPosition::new(rect.x + offset_x - border_width, rect.y + offset_y - border_width));

pin_window.set_bac_image(slint::Image::from_rgba8(img));
pin_window.set_img_x(rect.x / scale_factor);
pin_window.set_img_y(rect.y / scale_factor);
pin_window.set_img_width(rect.width / scale_factor);
pin_window.set_img_height(rect.height / scale_factor);
pin_window.set_img_x(rect.x);
pin_window.set_img_y(rect.y);
pin_window.set_img_width(rect.width);
pin_window.set_img_height(rect.height);

let zoom_delta = AppConfig::global()
.lock()
Expand All @@ -62,10 +59,11 @@ impl PinWin {
let mut img_width = pin_window_clone.get_img_width();
let mut img_height = pin_window_clone.get_img_height();
let zoom_factor = pin_window_clone.get_zoom_factor() as f32 / 100.;
let scale_factor = pin_window_clone.window().scale_factor();

let limit_px = 5.;
let img_width_px = img_width * zoom_factor;
let img_height_px = img_height * zoom_factor;
let img_width_px = (img_width as f32 * zoom_factor).ceil();
let img_height_px = (img_height as f32 * zoom_factor).ceil();
match mouse_direction {
Direction::Center => {
let is_stick_x = pin_window_clone.get_is_stick_x();
Expand All @@ -82,57 +80,57 @@ impl PinWin {
},
Direction::Upper => {
if (img_height_px - delta_y) < limit_px { delta_y = img_height_px - limit_px; }
img_y += delta_y / zoom_factor;
img_height -= delta_y / zoom_factor;
img_y += (delta_y / zoom_factor * scale_factor) as i32;
img_height -= (delta_y / zoom_factor * scale_factor) as i32;
delta_x = 0.;
},
Direction::Lower => {
if (img_height_px + delta_y) < limit_px { delta_y = limit_px - img_height_px; }
pin_window_clone.set_delta_img_height(delta_y / zoom_factor);
pin_window_clone.set_delta_img_height((delta_y / zoom_factor * scale_factor) as i32);
delta_x = 0.;
delta_y = 0.;
},
Direction::Left => {
if (img_width_px - delta_x) < limit_px { delta_x = img_width_px - limit_px; }
img_x += delta_x / zoom_factor;
img_width -= delta_x / zoom_factor;
img_x += (delta_x / zoom_factor * scale_factor) as i32;
img_width -= (delta_x / zoom_factor * scale_factor) as i32;
delta_y = 0.;
},
Direction::Right => {
if (img_width_px + delta_x) < limit_px { delta_x = limit_px - img_width_px; }
pin_window_clone.set_delta_img_width(delta_x / zoom_factor);
pin_window_clone.set_delta_img_width((delta_x / zoom_factor * scale_factor) as i32);
delta_x = 0.;
delta_y = 0.;
},
Direction::LeftUpper => {
if (img_width_px - delta_x) < limit_px { delta_x = img_width_px - limit_px; }
if (img_height_px - delta_y) < limit_px { delta_y = img_height_px - limit_px; }
img_x += delta_x / zoom_factor;
img_y += delta_y / zoom_factor;
img_width -= delta_x / zoom_factor;
img_height -= delta_y / zoom_factor;
img_x += (delta_x / zoom_factor * scale_factor) as i32;
img_y += (delta_y / zoom_factor * scale_factor) as i32;
img_width -= (delta_x / zoom_factor * scale_factor) as i32;
img_height -= (delta_y / zoom_factor * scale_factor) as i32;
},
Direction::LeftLower => {
if (img_width_px - delta_x) < limit_px { delta_x = img_width_px - limit_px; }
if (img_height_px + delta_y) < limit_px { delta_y = limit_px - img_height_px; }
img_x += delta_x / zoom_factor;
img_width -= delta_x / zoom_factor;
pin_window_clone.set_delta_img_height(delta_y / zoom_factor);
img_x += (delta_x / zoom_factor * scale_factor) as i32;
img_width -= (delta_x / zoom_factor * scale_factor) as i32;
pin_window_clone.set_delta_img_height((delta_y / zoom_factor * scale_factor) as i32);
delta_y = 0.;
},
Direction::RightUpper => {
if (img_width_px + delta_x) < limit_px { delta_x = limit_px - img_width_px; }
if (img_height_px - delta_y) < limit_px { delta_y = img_height_px - limit_px; }
img_y += delta_y / zoom_factor;
pin_window_clone.set_delta_img_width(delta_x / zoom_factor);
img_height -= delta_y / zoom_factor;
img_y += (delta_y / zoom_factor * scale_factor) as i32;
pin_window_clone.set_delta_img_width((delta_x / zoom_factor * scale_factor) as i32);
img_height -= (delta_y / zoom_factor * scale_factor) as i32;
delta_x = 0.;
},
Direction::RightLower => {
if (img_width_px + delta_x) < limit_px { delta_x = limit_px - img_width_px; }
if (img_height_px + delta_y) < limit_px { delta_y = limit_px - img_height_px; }
pin_window_clone.set_delta_img_width(delta_x / zoom_factor);
pin_window_clone.set_delta_img_height(delta_y / zoom_factor);
pin_window_clone.set_delta_img_width((delta_x / zoom_factor * scale_factor) as i32);
pin_window_clone.set_delta_img_height((delta_y / zoom_factor * scale_factor) as i32);
delta_x = 0.;
delta_y = 0.;
},
Expand Down Expand Up @@ -212,8 +210,7 @@ impl PinWin {
let pin_window_clone = pin_window.as_weak();
pin_window.on_save(move || {
if let Some(pin_window) = pin_window_clone.upgrade() {
let scale_factor = pin_window.get_scale_factor();
let border_width = (pin_window.get_win_border_width() * scale_factor).ceil() as u32;
let border_width = pin_window.get_win_border_width() as u32;

match pin_window.window().take_snapshot() {
Ok(buffer) => {
Expand Down Expand Up @@ -252,8 +249,7 @@ impl PinWin {
let pin_window_clone = pin_window.as_weak();
pin_window.on_copy(move || {
if let Some(pin_window) = pin_window_clone.upgrade() {
let scale_factor = pin_window.get_scale_factor();
let border_width = (pin_window.get_win_border_width() * scale_factor).ceil() as u32;
let border_width = pin_window.get_win_border_width() as u32;

match pin_window.window().take_snapshot() {
Ok(buffer) => {
Expand Down Expand Up @@ -354,13 +350,6 @@ impl PinWin {

let _ = pin_window.show();

{ // trick: fix the bug of error scale_factor
let scale_factor = pin_window.window().scale_factor();
if scale_factor != true_scale_factor {
pin_window.set_extra_zoom_factor(((scale_factor / true_scale_factor) * 100.) as i32);
}
}

Ok(PinWin {
_id: id,
pin_window,
Expand Down
2 changes: 1 addition & 1 deletion src/module/screen_shotter/shotter_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::util::{file_util, img_util, log_util};
pub struct ShotterConfig {
pub pos_x: i32,
pub pos_y: i32,
pub rect: (f32, f32, f32, f32),
pub rect: (i32, i32, i32, i32),
pub zoom_factor: i32,
}

Expand Down
Loading

0 comments on commit a5933f0

Please sign in to comment.