mirror of
https://github.com/penpot/penpot.git
synced 2026-08-26 22:58:43 +00:00
♻️ Refactor rulers into ui modules (#10461)
* ♻️ Move rulers rendering to ui submodule * ♻️ Refactor RulerState into UIState
This commit is contained in:
parent
b82ab0c830
commit
2e32a8743e
@ -7,7 +7,6 @@ pub mod grid_layout;
|
|||||||
mod images;
|
mod images;
|
||||||
mod options;
|
mod options;
|
||||||
pub mod pdf;
|
pub mod pdf;
|
||||||
pub mod rulers;
|
|
||||||
mod shadows;
|
mod shadows;
|
||||||
pub mod shape_renderer;
|
pub mod shape_renderer;
|
||||||
mod strokes;
|
mod strokes;
|
||||||
@ -30,7 +29,7 @@ use crate::shapes::{
|
|||||||
all_with_ancestors, radius_to_sigma, Blur, BlurType, Corners, Fill, Shadow, Shape, SolidColor,
|
all_with_ancestors, radius_to_sigma, Blur, BlurType, Corners, Fill, Shadow, Shape, SolidColor,
|
||||||
Stroke, StrokeKind, TextContent, Type,
|
Stroke, StrokeKind, TextContent, Type,
|
||||||
};
|
};
|
||||||
use crate::state::{RulerState, ShapesPoolMutRef, ShapesPoolRef};
|
use crate::state::{ShapesPoolMutRef, ShapesPoolRef};
|
||||||
use crate::tiles::{self, PendingTiles, TileRect};
|
use crate::tiles::{self, PendingTiles, TileRect};
|
||||||
use crate::uuid::Uuid;
|
use crate::uuid::Uuid;
|
||||||
use crate::view::Viewbox;
|
use crate::view::Viewbox;
|
||||||
@ -374,7 +373,6 @@ pub(crate) struct RenderState {
|
|||||||
pub nested_blurs: Vec<Option<Blur>>, // FIXME: why is this an option?
|
pub nested_blurs: Vec<Option<Blur>>, // FIXME: why is this an option?
|
||||||
pub nested_shadows: Vec<Vec<Shadow>>,
|
pub nested_shadows: Vec<Vec<Shadow>>,
|
||||||
pub show_grid: Option<Uuid>,
|
pub show_grid: Option<Uuid>,
|
||||||
pub rulers: RulerState,
|
|
||||||
pub focus_mode: FocusMode,
|
pub focus_mode: FocusMode,
|
||||||
/// Viewer-only whitelist for fixed-scroll layer passes.
|
/// Viewer-only whitelist for fixed-scroll layer passes.
|
||||||
pub include_filter: Option<HashSet<Uuid>>,
|
pub include_filter: Option<HashSet<Uuid>>,
|
||||||
@ -584,7 +582,6 @@ impl RenderState {
|
|||||||
nested_blurs: vec![],
|
nested_blurs: vec![],
|
||||||
nested_shadows: vec![],
|
nested_shadows: vec![],
|
||||||
show_grid: None,
|
show_grid: None,
|
||||||
rulers: RulerState::default(),
|
|
||||||
focus_mode: FocusMode::new(),
|
focus_mode: FocusMode::new(),
|
||||||
include_filter: None,
|
include_filter: None,
|
||||||
viewer_render_root: None,
|
viewer_render_root: None,
|
||||||
|
|||||||
@ -2,9 +2,10 @@ use skia_safe::{self as skia, Color4f};
|
|||||||
|
|
||||||
use super::{RenderState, ShapesPoolRef, SurfaceId};
|
use super::{RenderState, ShapesPoolRef, SurfaceId};
|
||||||
use crate::globals::get_ui_state;
|
use crate::globals::get_ui_state;
|
||||||
use crate::render::{grid_layout, rulers};
|
use crate::render::grid_layout;
|
||||||
use crate::shapes::{Layout, Type};
|
use crate::shapes::{Layout, Type};
|
||||||
pub mod guides;
|
pub mod guides;
|
||||||
|
pub mod rulers;
|
||||||
|
|
||||||
pub fn render(render_state: &mut RenderState, shapes: ShapesPoolRef) {
|
pub fn render(render_state: &mut RenderState, shapes: ShapesPoolRef) {
|
||||||
let canvas = render_state.surfaces.canvas(SurfaceId::UI);
|
let canvas = render_state.surfaces.canvas(SurfaceId::UI);
|
||||||
@ -63,8 +64,12 @@ pub fn render(render_state: &mut RenderState, shapes: ShapesPoolRef) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let viewbox = render_state.viewbox;
|
let viewbox = render_state.viewbox;
|
||||||
let ruler_state = render_state.rulers;
|
rulers::render(
|
||||||
rulers::render(canvas, viewbox, &render_state.fonts, &ruler_state);
|
canvas,
|
||||||
|
viewbox,
|
||||||
|
&render_state.fonts,
|
||||||
|
get_ui_state().ruler_state(),
|
||||||
|
);
|
||||||
|
|
||||||
// Width of the ruler bars in document coordinates.
|
// Width of the ruler bars in document coordinates.
|
||||||
// Note that when rulers are hidden, guides are not shown either, so we
|
// Note that when rulers are hidden, guides are not shown either, so we
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
use skia_safe::{self as skia, Color, Font, Paint, PaintStyle, PathFillType, Point, RRect, Rect};
|
use skia_safe::{self as skia, Color, Font, Paint, PaintStyle, PathFillType, Point, RRect, Rect};
|
||||||
|
|
||||||
use super::fonts::FontStore;
|
use crate::render::fonts::FontStore;
|
||||||
use crate::state::RulerState;
|
use crate::state::RulerState;
|
||||||
use crate::view::Viewbox;
|
use crate::view::Viewbox;
|
||||||
|
|
||||||
@ -1,13 +1,12 @@
|
|||||||
use skia_safe::{self as skia, textlayout::FontCollection, Path, Point};
|
use skia_safe::{self as skia, textlayout::FontCollection, Path, Point};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
mod rulers;
|
|
||||||
mod shapes_pool;
|
mod shapes_pool;
|
||||||
mod text_editor;
|
mod text_editor;
|
||||||
mod ui;
|
mod ui;
|
||||||
pub use rulers::RulerState;
|
|
||||||
pub use shapes_pool::{ShapesPool, ShapesPoolMutRef, ShapesPoolRef};
|
pub use shapes_pool::{ShapesPool, ShapesPoolMutRef, ShapesPoolRef};
|
||||||
pub use text_editor::*;
|
pub use text_editor::*;
|
||||||
|
pub use ui::RulerState;
|
||||||
pub use ui::UIState;
|
pub use ui::UIState;
|
||||||
|
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
|
|||||||
@ -1,42 +0,0 @@
|
|||||||
use skia_safe::{self as skia, Rect};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct RulerState {
|
|
||||||
pub visible: bool,
|
|
||||||
// The rounded canvas frame/border. Drawn even when `visible` is false
|
|
||||||
// (rulers toggled off), but hidden in hide-UI mode.
|
|
||||||
pub frame: bool,
|
|
||||||
pub offset_x: f32,
|
|
||||||
pub offset_y: f32,
|
|
||||||
pub selection: Option<Rect>,
|
|
||||||
pub bg_color: skia::Color,
|
|
||||||
pub border_color: skia::Color,
|
|
||||||
pub label_color: skia::Color,
|
|
||||||
pub accent_color: skia::Color,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for RulerState {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
visible: false,
|
|
||||||
frame: true,
|
|
||||||
offset_x: 0.0,
|
|
||||||
offset_y: 0.0,
|
|
||||||
selection: None,
|
|
||||||
bg_color: skia::Color::from_argb(0xff, 0x18, 0x18, 0x1a),
|
|
||||||
border_color: skia::Color::from_argb(0xff, 0x2e, 0x2e, 0x36),
|
|
||||||
label_color: skia::Color::from_argb(0xff, 0xb1, 0xb2, 0xb5),
|
|
||||||
accent_color: skia::Color::from_argb(0xff, 0x91, 0xff, 0x11),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RulerState {
|
|
||||||
pub fn set_selection(&mut self, has: bool, x: f32, y: f32, w: f32, h: f32) {
|
|
||||||
self.selection = if has {
|
|
||||||
Some(Rect::from_xywh(x, y, w, h))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::ui::{Guide, GuideKind};
|
use crate::ui::{Color, Guide, GuideKind, Rect};
|
||||||
|
|
||||||
pub struct GuidePool {
|
pub struct GuidePool {
|
||||||
horizontal: Vec<Guide>,
|
horizontal: Vec<Guide>,
|
||||||
@ -78,15 +78,64 @@ impl GuidePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for GuidePool {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct RulerState {
|
||||||
|
pub visible: bool,
|
||||||
|
// The rounded canvas frame/border. Drawn even when `visible` is false
|
||||||
|
// (rulers toggled off), but hidden in hide-UI mode.
|
||||||
|
pub frame: bool,
|
||||||
|
pub offset_x: f32,
|
||||||
|
pub offset_y: f32,
|
||||||
|
pub selection: Option<Rect>,
|
||||||
|
pub bg_color: Color,
|
||||||
|
pub border_color: Color,
|
||||||
|
pub label_color: Color,
|
||||||
|
pub accent_color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RulerState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
visible: false,
|
||||||
|
frame: true,
|
||||||
|
offset_x: 0.0,
|
||||||
|
offset_y: 0.0,
|
||||||
|
selection: None,
|
||||||
|
bg_color: Color::from_argb(0xff, 0x18, 0x18, 0x1a),
|
||||||
|
border_color: Color::from_argb(0xff, 0x2e, 0x2e, 0x36),
|
||||||
|
label_color: Color::from_argb(0xff, 0xb1, 0xb2, 0xb5),
|
||||||
|
accent_color: Color::from_argb(0xff, 0x91, 0xff, 0x11),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RulerState {
|
||||||
|
pub fn set_selection(&mut self, has: bool, x: f32, y: f32, w: f32, h: f32) {
|
||||||
|
self.selection = if has {
|
||||||
|
Some(Rect::from_xywh(x, y, w, h))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UIState {
|
pub struct UIState {
|
||||||
guides: GuidePool,
|
guides: GuidePool,
|
||||||
// TODO: show grid, rulers, etc.
|
rulers: RulerState,
|
||||||
|
// TODO: show grid
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UIState {
|
impl UIState {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
guides: GuidePool::new(),
|
guides: GuidePool::default(),
|
||||||
|
rulers: RulerState::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +150,14 @@ impl UIState {
|
|||||||
pub fn find_guide_at(&self, x: f32, y: f32, zoom: f32, tolerance: f32) -> Option<&Guide> {
|
pub fn find_guide_at(&self, x: f32, y: f32, zoom: f32, tolerance: f32) -> Option<&Guide> {
|
||||||
self.guides.find_at(x, y, zoom, tolerance)
|
self.guides.find_at(x, y, zoom, tolerance)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ruler_state(&self) -> &RulerState {
|
||||||
|
&self.rulers
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ruler_state_mut(&mut self) -> &mut RulerState {
|
||||||
|
&mut self.rulers
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::shapes::Color;
|
pub use crate::math::Rect;
|
||||||
|
pub use crate::shapes::Color;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum GuideKind {
|
pub enum GuideKind {
|
||||||
|
|||||||
@ -5,7 +5,6 @@ pub mod fonts;
|
|||||||
pub mod layouts;
|
pub mod layouts;
|
||||||
pub mod mem;
|
pub mod mem;
|
||||||
pub mod paths;
|
pub mod paths;
|
||||||
pub mod rulers;
|
|
||||||
pub mod shadows;
|
pub mod shadows;
|
||||||
pub mod shapes;
|
pub mod shapes;
|
||||||
pub mod strokes;
|
pub mod strokes;
|
||||||
|
|||||||
@ -1,49 +0,0 @@
|
|||||||
use macros::wasm_error;
|
|
||||||
use skia_safe::{self as skia};
|
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
|
||||||
use crate::error::{Error, Result};
|
|
||||||
use crate::get_render_state;
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
#[wasm_error]
|
|
||||||
pub extern "C" fn set_rulers_visible(visible: u32) -> Result<()> {
|
|
||||||
get_render_state().rulers.visible = visible != 0;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
#[wasm_error]
|
|
||||||
pub extern "C" fn set_rulers_frame_visible(visible: u32) -> Result<()> {
|
|
||||||
get_render_state().rulers.frame = visible != 0;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
#[wasm_error]
|
|
||||||
pub extern "C" fn set_rulers_offsets(offset_x: f32, offset_y: f32) -> Result<()> {
|
|
||||||
let r = &mut get_render_state().rulers;
|
|
||||||
r.offset_x = offset_x;
|
|
||||||
r.offset_y = offset_y;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
#[wasm_error]
|
|
||||||
pub extern "C" fn set_rulers_selection(has: u32, x: f32, y: f32, w: f32, h: f32) -> Result<()> {
|
|
||||||
get_render_state()
|
|
||||||
.rulers
|
|
||||||
.set_selection(has != 0, x, y, w, h);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
#[wasm_error]
|
|
||||||
pub extern "C" fn set_rulers_colors(bg: u32, border: u32, label: u32, accent: u32) -> Result<()> {
|
|
||||||
let r = &mut get_render_state().rulers;
|
|
||||||
r.bg_color = skia::Color::new(bg);
|
|
||||||
r.border_color = skia::Color::new(border);
|
|
||||||
r.label_color = skia::Color::new(label);
|
|
||||||
r.accent_color = skia::Color::new(accent);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@ -3,7 +3,7 @@ use crate::with_state;
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::{Error, Result},
|
error::{Error, Result},
|
||||||
globals::{get_render_state, get_ui_state},
|
globals::{get_render_state, get_ui_state},
|
||||||
ui::{Guide, GuideKind},
|
ui::{Color, Guide, GuideKind},
|
||||||
};
|
};
|
||||||
use macros::{wasm_error, ToJs};
|
use macros::{wasm_error, ToJs};
|
||||||
|
|
||||||
@ -122,3 +122,46 @@ pub extern "C" fn find_guide_at(x: f32, y: f32, zoom: f32, tolerance: f32) -> Re
|
|||||||
.map(|guide| guide.index as i32)
|
.map(|guide| guide.index as i32)
|
||||||
.unwrap_or(-1))
|
.unwrap_or(-1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[wasm_error]
|
||||||
|
pub extern "C" fn set_rulers_visible(visible: u32) -> Result<()> {
|
||||||
|
get_ui_state().ruler_state_mut().visible = visible != 0;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[wasm_error]
|
||||||
|
pub extern "C" fn set_rulers_frame_visible(visible: u32) -> Result<()> {
|
||||||
|
get_ui_state().ruler_state_mut().frame = visible != 0;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[wasm_error]
|
||||||
|
pub extern "C" fn set_rulers_offsets(offset_x: f32, offset_y: f32) -> Result<()> {
|
||||||
|
let r = &mut get_ui_state().ruler_state_mut();
|
||||||
|
r.offset_x = offset_x;
|
||||||
|
r.offset_y = offset_y;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[wasm_error]
|
||||||
|
pub extern "C" fn set_rulers_selection(has: u32, x: f32, y: f32, w: f32, h: f32) -> Result<()> {
|
||||||
|
get_ui_state()
|
||||||
|
.ruler_state_mut()
|
||||||
|
.set_selection(has != 0, x, y, w, h);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[wasm_error]
|
||||||
|
pub extern "C" fn set_rulers_colors(bg: u32, border: u32, label: u32, accent: u32) -> Result<()> {
|
||||||
|
let r = &mut get_ui_state().ruler_state_mut();
|
||||||
|
r.bg_color = Color::new(bg);
|
||||||
|
r.border_color = Color::new(border);
|
||||||
|
r.label_color = Color::new(label);
|
||||||
|
r.accent_color = Color::new(accent);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user