Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6a19fe5

Browse files
committedApr 22, 2025
feat: implement extending side
1 parent 56fbab6 commit 6a19fe5

File tree

5 files changed

+67
-58
lines changed

5 files changed

+67
-58
lines changed
 

‎default.kdl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ keys {
1616

1717
select-full-screen key=<f11>
1818

19-
// move a side by 1px
19+
// move the selection in a direction by 1px
2020
move left 1 key=h
2121
move left 1 key=<left>
2222
move down 1 key=j
@@ -46,45 +46,45 @@ keys {
4646
shrink right 1 mod=ctrl key=l
4747
shrink right 1 mod=ctrl key=<right>
4848

49-
// move rectangle in direction by 5px
50-
move left 5 mod=alt key=h
51-
move left 5 mod=alt key=<left>
52-
move down 5 mod=alt key=j
53-
move down 5 mod=alt key=<down>
54-
move up 5 mod=alt key=k
55-
move up 5 mod=alt key=<up>
56-
move right 5 mod=alt key=l
57-
move right 5 mod=alt key=<right>
49+
// move rectangle in direction by 10px
50+
move left 10 mod=alt key=h
51+
move left 10 mod=alt key=<left>
52+
move down 10 mod=alt key=j
53+
move down 10 mod=alt key=<down>
54+
move up 10 mod=alt key=k
55+
move up 10 mod=alt key=<up>
56+
move right 10 mod=alt key=l
57+
move right 10 mod=alt key=<right>
5858

59-
// extend a side by 5px
60-
extend left 5 mod=alt key=H
61-
extend left 5 mod=alt key=<left>
62-
extend down 5 mod=alt key=J
63-
extend down 5 mod=alt key=<down>
64-
extend up 5 mod=alt key=K
65-
extend up 5 mod=alt key=<up>
66-
extend right 5 mod=alt key=L
67-
extend right 5 mod=alt key=<right>
59+
// extend a side by 10px
60+
extend left 10 mod=alt key=H
61+
extend left 10 mod=alt key=<left>
62+
extend down 10 mod=alt key=J
63+
extend down 10 mod=alt key=<down>
64+
extend up 10 mod=alt key=K
65+
extend up 10 mod=alt key=<up>
66+
extend right 10 mod=alt key=L
67+
extend right 10 mod=alt key=<right>
6868

69-
// shrink a side by 5px
70-
shrink left 5 mod="ctrl + alt" key=h
71-
shrink left 5 mod="ctrl + alt" key=<left>
72-
shrink down 5 mod="ctrl + alt" key=j
73-
shrink down 5 mod="ctrl + alt" key=<down>
74-
shrink up 5 mod="ctrl + alt" key=k
75-
shrink up 5 mod="ctrl + alt" key=<up>
76-
shrink right 5 mod="ctrl + alt" key=l
77-
shrink right 5 mod="ctrl + alt" key=<right>
69+
// shrink a side by 10px
70+
shrink left 10 mod=ctrl+alt key=h
71+
shrink left 10 mod=ctrl+alt key=<left>
72+
shrink down 10 mod=ctrl+alt key=j
73+
shrink down 10 mod=ctrl+alt key=<down>
74+
shrink up 10 mod=ctrl+alt key=k
75+
shrink up 10 mod=ctrl+alt key=<up>
76+
shrink right 10 mod=ctrl+alt key=l
77+
shrink right 10 mod=ctrl+alt key=<right>
7878

7979
// move selection as far as it can go
80-
move left #inf key=gh
81-
move left #inf key=g<left>
82-
move down #inf key=gj
83-
move down #inf key=g<down>
84-
move up #inf key=gk
85-
move up #inf key=g<up>
86-
move right #inf key=gl
87-
move right #inf key=g<right>
80+
move left key=gh
81+
move left key=g<left>
82+
move down key=gj
83+
move down key=g<down>
84+
move up key=gk
85+
move up key=g<up>
86+
move right key=gl
87+
move right key=g<right>
8888
}
8989

9090
theme {

‎src/app.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use iced::widget::{self, Column, Space, Stack, canvas, column, container, row};
1717
use iced::{Background, Color, Element, Font, Length, Point, Rectangle, Size, Task};
1818

1919
use crate::background_image::BackgroundImage;
20-
use crate::corners::{Corner, Direction, Side, SideOrCorner};
20+
use crate::corners::{Direction, Side, SideOrCorner};
2121
use crate::rectangle::RectangleExt;
2222
use crate::selection::{Selection, SelectionStatus};
2323

@@ -351,7 +351,7 @@ impl App {
351351
Direction::Down => {
352352
sel.with_y(|y| (y + amount).min(image_height - sel.rect.height))
353353
}
354-
Direction::Left => sel.with_x(|x| dbg!(dbg!((dbg!(x) - amount)).max(0.0))),
354+
Direction::Left => sel.with_x(|x| (x - amount).max(0.0)),
355355
Direction::Right => {
356356
sel.with_x(|x| (x + amount).min(image_width - sel.rect.width))
357357
}
@@ -362,21 +362,35 @@ impl App {
362362
self.error("Nothing is selected.");
363363
return Task::none();
364364
};
365+
let (image_width, image_height, _) = self.screenshot.raw();
366+
let image_height = image_height as f32;
367+
let image_width = image_width as f32;
365368
let sel = selection.norm();
366369
let amount = amount as f32;
367370

368371
*selection = match direction {
369-
Direction::Up => todo!(),
370-
Direction::Down => todo!(),
371-
Direction::Left => todo!(),
372-
Direction::Right => todo!(),
372+
Direction::Up => sel
373+
.with_y(|y| (y - amount).max(0.0))
374+
.with_height(|h| (h + amount).min(sel.rect.y + sel.rect.height)),
375+
Direction::Down => {
376+
sel.with_height(|h| (h + amount).min(image_height - sel.rect.y))
377+
}
378+
Direction::Left => sel
379+
.with_x(|x| (x - amount).max(0.0))
380+
.with_width(|w| (w + amount).min(sel.rect.x + sel.rect.width)),
381+
Direction::Right => {
382+
sel.with_width(|w| (w + amount).min(image_width - sel.rect.x))
383+
}
373384
}
374385
}
375386
KeyAction::Shrink(direction, amount) => {
376387
let Some(selection) = self.selection.as_mut() else {
377388
self.error("Nothing is selected.");
378389
return Task::none();
379390
};
391+
let (image_width, image_height, _) = self.screenshot.raw();
392+
let image_height = image_height as f32;
393+
let image_width = image_width as f32;
380394
let sel = selection.norm();
381395
let amount = amount as f32;
382396

‎src/canvas.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//! The canvas handles drawing the selection frame
22
use iced::Event::{Keyboard, Mouse};
3-
use iced::advanced::debug::core::SmolStr;
43
use iced::keyboard::Event::KeyPressed;
54
use iced::keyboard::Event::KeyReleased;
6-
use iced::keyboard::Key::{self, Character, Named};
5+
use iced::keyboard::Key::{self, Named};
76
use iced::keyboard::Modifiers;
8-
use iced::keyboard::Modifiers as Mods;
9-
use iced::keyboard::key::Named::F11;
10-
use iced::keyboard::key::Named::{Enter, Escape, Shift};
7+
use iced::keyboard::key::Named::Shift;
118
use iced::mouse::Button::{Left, Right};
129
use iced::mouse::Event::ButtonPressed;
1310
use iced::mouse::Event::ButtonReleased;

‎src/config/macros.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,10 @@ macro_rules! declare_key_options {
239239
(
240240
$(
241241
$(#[$doc:meta])*
242-
$KeyOption:ident $({
243-
$(
244-
$(#[$attr:meta])*
245-
$field:ident: $Argument:ty,
246-
)+
247-
})?
242+
$KeyOption:ident $({$(
243+
$(#[$attr:meta])*
244+
$field:ident: $Argument:ty $(= $default:expr)?,
245+
)+})?
248246
),* $(,)?
249247
) => {
250248
/// A list of keybindings which exist in the app
@@ -257,8 +255,8 @@ macro_rules! declare_key_options {
257255
$KeyOption(
258256
$($(
259257
$(#[$attr])*
260-
#[knus(argument)] $Argument,
261-
)*)?
258+
#[knus(argument)] $(#[knus(default = $default)])? $Argument,
259+
)+)?
262260
#[knus(property(name = "key"), str)] $crate::config::key::KeySequence,
263261
#[knus(default, property(name = "mod"), str)] $crate::config::key::KeyMods,
264262
),

‎src/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ crate::declare_key_options! {
6464
/// Shift the selection in the given direction by pixels
6565
Move {
6666
direction: Direction,
67-
amount: u32,
67+
amount: u32 = u32::MAX,
6868
},
6969
/// Increase the size of the selection in the given direction by pixels
7070
Extend {
7171
direction: Direction,
72-
amount: u32,
72+
amount: u32 = u32::MAX,
7373
},
7474
/// Decrease the size of the selection in the given direction by pixels
7575
Shrink {
7676
direction: Direction,
77-
amount: u32,
77+
amount: u32 = u32::MAX,
7878
},
7979
}
8080

0 commit comments

Comments
 (0)
Please sign in to comment.