Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ This binds **Shift+Print** to `kirie capture`, so your desktop's own screenshot
`kirie capture` freezes every screen. Drag to cut out a region, or click to take the window
under the pointer (on GNOME with the extension, COSMIC, KDE Plasma, sway and Hyprland).
**Take this screen** in the hint at the bottom takes the whole screen; where Kirie cannot see
windows, a click does.
windows, a click does. With several screens, **Take all screens** takes them together, as Ctrl+A
does.
A loupe beside the pointer shows the pixels around it, magnified, with
its position and color, so edges land on the exact pixel. Then mark it up with the toolbar under
the selection.
Expand Down
31 changes: 30 additions & 1 deletion crates/kirie-annotate/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ impl Draft {
/// The mark as it would be if released now, to show while drawing.
pub fn preview(&self) -> Option<Kind> {
match self {
Draft::Span { tool, start, end }
if *tool != Tool::Bubble && start.distance(*end) < MIN_DRAG =>
{
None
}
Draft::Span { tool, start, end } => span_kind(*tool, *start, *end),
Draft::Stroke { tool, points } => match tool {
Tool::Highlighter => Some(Kind::Highlighter {
Expand Down Expand Up @@ -369,7 +374,6 @@ impl Draft {
/// The edit the draft makes when released, if it made anything.
pub fn finish(self, paint: Paint) -> Option<Op> {
match self {
Draft::Span { start, end, .. } if start.distance(end) < MIN_DRAG => None,
Draft::Marquee { .. }
| Draft::Span {
tool: Tool::Bubble, ..
Expand Down Expand Up @@ -533,6 +537,31 @@ mod tests {
);
}

#[test]
fn a_press_without_a_drag_shows_nothing_until_it_would_make_something() {
for tool in [Tool::Arrow, Tool::Line, Tool::Rectangle, Tool::Blur] {
let Press::Draft(draft) = press(
tool,
Point::ORIGIN,
&doc(),
Paint::default(),
4.0,
&[],
false,
) else {
panic!("{tool:?} started no draft");
};
let pressed = draft.drag(Point::new(1.0, 1.0), false);
assert_eq!(pressed.preview(), None, "{tool:?}");
assert!(
pressed
.drag(Point::new(40.0, 30.0), false)
.preview()
.is_some()
);
}
}

#[test]
fn shift_snaps() {
let draft = Draft::Span {
Expand Down
185 changes: 185 additions & 0 deletions crates/kirie-ui/src/checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//! Light and dark squares, which show through what is translucent.

use iced::widget::canvas::{self, Frame, Geometry, Path};
use iced::{Color, Point, Rectangle, Renderer, Size};

const LIGHT: Color = crate::rgb(0xf2, 0xf2, 0xf2);
const DARK: Color = crate::rgb(0xc8, 0xc8, 0xc8);
const SEGMENTS: u16 = 48;

/// Fills `area`, which spans `size` from the origin, with squares `cell` wide.
pub fn rectangle(frame: &mut Frame, area: &Path, size: Size, cell: f32) {
frame.fill(area, LIGHT);
let bounds = Rectangle::new(Point::ORIGIN, size);
let dark = Path::new(|path| {
for square in dark_squares(bounds, cell) {
path.rectangle(square.position(), square.size());
}
});
frame.fill(&dark, DARK);
}

/// Fills the disc at `center` with squares `cell` wide.
pub fn disc(frame: &mut Frame, center: Point, radius: f32, cell: f32) {
frame.fill(&Path::circle(center, radius), LIGHT);
let outline: Vec<Point> = (0..SEGMENTS)
.map(|step| {
let angle = f32::from(step) / f32::from(SEGMENTS) * std::f32::consts::TAU;
Point::new(
center.x + radius * angle.cos(),
center.y + radius * angle.sin(),
)
})
.collect();
let bounds = Rectangle::new(
Point::new(center.x - radius, center.y - radius),
Size::new(2.0 * radius, 2.0 * radius),
);
let dark = Path::new(|path| {
for square in dark_squares(bounds, cell) {
let piece = clip(&outline, square);
if let Some((&first, rest)) = piece.split_first() {
path.move_to(first);
for &point in rest {
path.line_to(point);
}
path.close();
}
}
});
frame.fill(&dark, DARK);
}

/// A disc of squares filling its bounds, to lay under a translucent dot.
#[derive(Debug, Clone, Copy)]
pub struct Disc;

impl<Message> canvas::Program<Message> for Disc {
type State = ();

fn draw(
&self,
(): &(),
renderer: &Renderer,
_: &iced::Theme,
bounds: Rectangle,
_: iced::mouse::Cursor,
) -> Vec<Geometry> {
let mut frame = Frame::new(renderer, bounds.size());
let side = bounds.width.min(bounds.height);
let center = frame.center();
disc(&mut frame, center, side / 2.0 - 0.5, side / 4.0);
vec![frame.into_geometry()]
}
}

/// Every other square of a grid of `cell` over `bounds`, cut to it.
fn dark_squares(bounds: Rectangle, cell: f32) -> impl Iterator<Item = Rectangle> {
let right = bounds.x + bounds.width;
let bottom = bounds.y + bounds.height;
let steps = move |from: f32, to: f32| {
std::iter::successors(Some(from), move |at| Some(at + cell))
.take_while(move |at| *at < to)
.enumerate()
};
steps(bounds.x, right).flat_map(move |(column, x)| {
steps(bounds.y, bottom)
.filter(move |(row, _)| (column + row).is_multiple_of(2))
.map(move |(_, y)| {
Rectangle::new(
Point::new(x, y),
Size::new(cell.min(right - x), cell.min(bottom - y)),
)
})
})
}

#[derive(Clone, Copy)]
enum Axis {
X,
Y,
}

/// The part of the convex `polygon` inside `rect`.
fn clip(polygon: &[Point], rect: Rectangle) -> Vec<Point> {
[
(Axis::X, rect.x, true),
(Axis::X, rect.x + rect.width, false),
(Axis::Y, rect.y, true),
(Axis::Y, rect.y + rect.height, false),
]
.into_iter()
.fold(polygon.to_vec(), |points, (axis, bound, above)| {
clip_to(&points, axis, bound, above)
})
}

/// The part of `points` on one side of the line where `axis` is `bound`.
fn clip_to(points: &[Point], axis: Axis, bound: f32, above: bool) -> Vec<Point> {
let coordinate = |point: Point| match axis {
Axis::X => point.x,
Axis::Y => point.y,
};
let inside = |point: Point| {
if above {
coordinate(point) >= bound
} else {
coordinate(point) <= bound
}
};
let crossing = |from: Point, to: Point| {
let t = (bound - coordinate(from)) / (coordinate(to) - coordinate(from));
Point::new(from.x + t * (to.x - from.x), from.y + t * (to.y - from.y))
};
let previous = points.iter().cycle().skip(points.len().saturating_sub(1));
points
.iter()
.zip(previous)
.flat_map(
|(&current, &previous)| match (inside(previous), inside(current)) {
(true, true) => vec![current],
(true, false) => vec![crossing(previous, current)],
(false, true) => vec![crossing(previous, current), current],
(false, false) => vec![],
},
)
.collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn clipping_keeps_what_is_inside() {
let square = [
Point::new(0.0, 0.0),
Point::new(4.0, 0.0),
Point::new(4.0, 4.0),
Point::new(0.0, 4.0),
];
let half = clip(&square, Rectangle::new(Point::ORIGIN, Size::new(2.0, 10.0)));
let xs: Vec<f32> = half.iter().map(|point| point.x).collect();
assert!(xs.iter().all(|&x| (0.0..=2.0).contains(&x)));
assert_eq!(half.len(), 4);
assert!(
clip(
&square,
Rectangle::new(Point::new(5.0, 5.0), Size::new(1.0, 1.0))
)
.is_empty()
);
}

#[test]
fn squares_alternate_and_stay_inside() {
let bounds = Rectangle::new(Point::ORIGIN, Size::new(10.0, 10.0));
let squares: Vec<Rectangle> = dark_squares(bounds, 4.0).collect();
assert_eq!(squares.len(), 5);
assert!(
squares.iter().all(|square| {
square.x + square.width <= 10.0 && square.y + square.height <= 10.0
})
);
}
}
1 change: 1 addition & 0 deletions crates/kirie-ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Kirie's design language: a calm, flat dark or light surface with one vermilion accent, on a
//! 4 px grid with soft radii. The overlay and the editor both draw from these tokens.

pub mod checker;
pub mod icon;
pub mod widget;

Expand Down
38 changes: 24 additions & 14 deletions crates/kirie-ui/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! they sit on.

use iced::overlay::menu;
use iced::widget::{button, container, pick_list, row, slider, svg, text, tooltip};
use iced::widget::{Canvas, button, container, pick_list, row, slider, stack, svg, text, tooltip};
use iced::{Background, Border, Color, Element, Length, Shadow, Vector};

use crate::checker;
use crate::icon::Icon;
use crate::{Palette, radius, space};

Expand Down Expand Up @@ -186,19 +187,7 @@ pub fn swatch<'a, Message: Clone + 'a>(
palette: Palette,
on_press: Message,
) -> Element<'a, Message> {
let dot = container(text(""))
.width(18)
.height(18)
.style(move |_| container::Style {
background: Some(Background::Color(color)),
border: Border {
color: Color::WHITE.scale_alpha(0.35),
width: 1.0,
radius: radius::PILL.into(),
},
..container::Style::default()
});
button(container(dot).center(Length::Fill))
button(container(color_dot(color, 18.0)).center(Length::Fill))
.width(28)
.height(28)
.padding(0)
Expand All @@ -223,6 +212,27 @@ pub fn swatch<'a, Message: Clone + 'a>(
.into()
}

/// A round dot of `color`, `side` wide, over light and dark squares when it is translucent.
pub fn color_dot<'a, Message: 'a>(color: Color, side: f32) -> Element<'a, Message> {
let dot = container(text(""))
.width(side)
.height(side)
.style(move |_| container::Style {
background: Some(Background::Color(color)),
border: Border {
color: Color::WHITE.scale_alpha(0.35),
width: 1.0,
radius: radius::PILL.into(),
},
..container::Style::default()
});
if color.a < 1.0 {
stack![Canvas::new(checker::Disc).width(side).height(side), dot].into()
} else {
dot.into()
}
}

/// A round button the size of a swatch, with an icon.
pub fn swatch_button<'a, Message: Clone + 'a>(
icon: Icon,
Expand Down
46 changes: 5 additions & 41 deletions crates/kirie/src/annotate/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

use iced::mouse::{self, Interaction};
use iced::widget::canvas::{self, Action, Frame, Geometry, Path, Stroke, gradient};
use iced::widget::{column, container, row, text, text_input};
use iced::widget::{column, row, text, text_input};
use iced::{Alignment, Background, Border, Color, Element, Length, Point, Rectangle, Renderer};
use kirie_annotate::{Rgba, StyleId};
use kirie_ui::checker;
use kirie_ui::icon::Icon;
use kirie_ui::widget::{icon_button, pill};
use kirie_ui::widget::{color_dot, icon_button, pill};
use kirie_ui::{Palette, radius, space};

use crate::annotate::paint::color_of;
Expand Down Expand Up @@ -232,18 +233,7 @@ pub fn view(picker: &Picker, palette: Palette) -> Element<'_, Event> {
Target::Change(Slot::Default(_)) => "Change this color",
Target::Change(Slot::Custom(_)) => "Change your color",
};
let preview = container(text("")).width(26).height(26).style({
let color = color_of(picker.color());
move |_| container::Style {
background: Some(Background::Color(color)),
border: Border {
color: Color::WHITE.scale_alpha(0.35),
width: 1.0,
radius: radius::PILL.into(),
},
..container::Style::default()
}
});
let preview = color_dot(color_of(picker.color()), 26.0);
let hex = text_input("#rrggbb", &picker.hex)
.on_input(Event::Hex)
.on_submit(Event::Apply)
Expand Down Expand Up @@ -444,7 +434,7 @@ impl canvas::Program<Event> for Field {
ring(&mut frame, &knob(x, size.height));
}
Area::Alpha(alpha) => {
checker(&mut frame, size);
checker::rectangle(&mut frame, &area, size, 7.0);
let solid = color_of(self.hsv.to_rgba());
let fade = gradient::Linear::new(Point::ORIGIN, Point::new(size.width, 0.0))
.add_stop(0.0, solid.scale_alpha(0.0))
Expand Down Expand Up @@ -480,32 +470,6 @@ fn knob(x: f32, height: f32) -> Path {
)
}

/// Light and dark squares, which show through what is translucent.
fn checker(frame: &mut Frame, size: iced::Size) {
const CELL: f32 = 7.0;
frame.fill(
&Path::rounded_rectangle(Point::ORIGIN, size, radius::CONTROL.into()),
Color::from_rgb8(0xf2, 0xf2, 0xf2),
);
let dark = Path::new(|path| {
let mut x = 0.0;
let mut column = 0u32;
while x < size.width {
let mut y = if column.is_multiple_of(2) { 0.0 } else { CELL };
while y < size.height {
path.rectangle(
Point::new(x, y),
iced::Size::new(CELL.min(size.width - x), CELL.min(size.height - y)),
);
y += 2.0 * CELL;
}
x += CELL;
column += 1;
}
});
frame.fill(&dark, Color::from_rgb8(0xc8, 0xc8, 0xc8));
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading