Skip to content

Commit

Permalink
Move cursor type conversion into blitz-dom
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Dec 23, 2024
1 parent 13ce23f commit 23fa191
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 78 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ accesskit = "0.15.0"
muda = { version = "0.11.5", default-features = false }
arboard = { version = "3.4.1", default-features = false }
keyboard-types = "0.7"
cursor-icon = "1"

# IO & Networking
url = "2.5.0"
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-dom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ url = { workspace = true, features = ["serde"] }

# Input
winit = { workspace = true }
cursor-icon = { workspace = true }

# Rendering
raw-window-handle = { workspace = true }
17 changes: 9 additions & 8 deletions packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::events::{apply_keypress_event, EventData, HitResult, RendererEvent};
use crate::layout::construct::collect_layout_children;
use crate::node::{ImageData, NodeSpecificData, Status, TextBrush};
use crate::stylo_to_cursor_icon::stylo_to_cursor_icon;
use crate::util::ImageType;
use crate::{ElementNodeData, Node, NodeData, TextNodeData};
use app_units::Au;
use blitz_traits::net::{DummyNetProvider, SharedProvider};
use blitz_traits::{ColorScheme, Viewport};
use cursor_icon::CursorIcon;
use markup5ever::local_name;
use parley::FontContext;
use peniko::kurbo;
Expand All @@ -30,7 +32,6 @@ use style::queries::values::PrefersColorScheme;
use style::selector_parser::ServoElementSnapshot;
use style::servo::media_queries::FontMetricsProvider;
use style::servo_arc::Arc as ServoArc;
use style::values::computed::ui::CursorKind;
use style::{
dom::{TDocument, TNode},
media_queries::{Device, MediaList},
Expand Down Expand Up @@ -1068,15 +1069,15 @@ impl Document {

pub fn add_element(&mut self) {}

pub fn get_cursor(&self) -> Option<CursorKind> {
pub fn get_cursor(&self) -> Option<CursorIcon> {
// todo: cache this on the node itself
let node = &self.nodes[self.get_hover_node_id()?];

let style = node.primary_styles()?;
let keyword = style.clone_cursor().keyword;
let keyword = stylo_to_cursor_icon(style.clone_cursor().keyword);

// Return cursor from style if it is non-auto
if keyword != CursorKind::Auto {
if keyword != CursorIcon::Default {
return Some(keyword);
}

Expand All @@ -1086,21 +1087,21 @@ impl Document {
.element_data()
.is_some_and(|e| e.text_input_data().is_some())
{
return Some(CursorKind::Text);
return Some(CursorIcon::Text);
}

// Use "pointer" cursor if any ancestor is a link
let mut maybe_node = Some(node);
while let Some(node) = maybe_node {
if node.is_link() {
return Some(CursorKind::Pointer);
return Some(CursorIcon::Pointer);
}

maybe_node = node.parent_node();
}

// Else fallback to auto cursor
Some(CursorKind::Auto)
// Else fallback to default cursor
Some(CursorIcon::Default)
}

/// Scroll a node by given x and y
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod node;
/// Implementations that interact with servo's style engine
pub mod stylo;

pub mod stylo_to_cursor_icon;
pub mod stylo_to_parley;

pub mod image;
Expand Down
48 changes: 48 additions & 0 deletions packages/blitz-dom/src/stylo_to_cursor_icon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use cursor_icon::CursorIcon;
use style::values::computed::ui::CursorKind as StyloCursorKind;

pub(crate) fn stylo_to_cursor_icon(cursor: StyloCursorKind) -> CursorIcon {
match cursor {
StyloCursorKind::None => todo!("set the cursor to none"),
StyloCursorKind::Default => CursorIcon::Default,
StyloCursorKind::Pointer => CursorIcon::Pointer,
StyloCursorKind::ContextMenu => CursorIcon::ContextMenu,
StyloCursorKind::Help => CursorIcon::Help,
StyloCursorKind::Progress => CursorIcon::Progress,
StyloCursorKind::Wait => CursorIcon::Wait,
StyloCursorKind::Cell => CursorIcon::Cell,
StyloCursorKind::Crosshair => CursorIcon::Crosshair,
StyloCursorKind::Text => CursorIcon::Text,
StyloCursorKind::VerticalText => CursorIcon::VerticalText,
StyloCursorKind::Alias => CursorIcon::Alias,
StyloCursorKind::Copy => CursorIcon::Copy,
StyloCursorKind::Move => CursorIcon::Move,
StyloCursorKind::NoDrop => CursorIcon::NoDrop,
StyloCursorKind::NotAllowed => CursorIcon::NotAllowed,
StyloCursorKind::Grab => CursorIcon::Grab,
StyloCursorKind::Grabbing => CursorIcon::Grabbing,
StyloCursorKind::EResize => CursorIcon::EResize,
StyloCursorKind::NResize => CursorIcon::NResize,
StyloCursorKind::NeResize => CursorIcon::NeResize,
StyloCursorKind::NwResize => CursorIcon::NwResize,
StyloCursorKind::SResize => CursorIcon::SResize,
StyloCursorKind::SeResize => CursorIcon::SeResize,
StyloCursorKind::SwResize => CursorIcon::SwResize,
StyloCursorKind::WResize => CursorIcon::WResize,
StyloCursorKind::EwResize => CursorIcon::EwResize,
StyloCursorKind::NsResize => CursorIcon::NsResize,
StyloCursorKind::NeswResize => CursorIcon::NeswResize,
StyloCursorKind::NwseResize => CursorIcon::NwseResize,
StyloCursorKind::ColResize => CursorIcon::ColResize,
StyloCursorKind::RowResize => CursorIcon::RowResize,
StyloCursorKind::AllScroll => CursorIcon::AllScroll,
StyloCursorKind::ZoomIn => CursorIcon::ZoomIn,
StyloCursorKind::ZoomOut => CursorIcon::ZoomOut,
StyloCursorKind::Auto => {
// todo: we should be the ones determining this based on the UA?
// https://developer.mozilla.org/en-US/docs/Web/CSS/cursor

CursorIcon::Default
}
}
}
3 changes: 0 additions & 3 deletions packages/blitz-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ tracing = ["dep:tracing", "blitz-dom/tracing"]
blitz-traits = { path = "../blitz-traits" }
blitz-dom = { path = "../blitz-dom", default-features = false }

# Servo dependencies
style = { workspace = true }

# Windowing & Input
winit = { workspace = true }
muda = { workspace = true, default-features = false, features = ["serde"], optional = true }
Expand Down
1 change: 0 additions & 1 deletion packages/blitz-shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
mod application;
mod event;
mod stylo_to_winit;
mod window;

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
Expand Down
63 changes: 0 additions & 63 deletions packages/blitz-shell/src/stylo_to_winit.rs

This file was deleted.

19 changes: 16 additions & 3 deletions packages/blitz-shell/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::event::{create_waker, BlitzEvent};
use crate::stylo_to_winit::{self, color_scheme_to_theme, theme_to_color_scheme};
use blitz_dom::events::{EventData, RendererEvent};
use blitz_dom::{DocumentLike, DocumentRenderer};
use blitz_traits::{Devtools, Viewport};
use blitz_traits::{ColorScheme, Devtools, Viewport};
use winit::keyboard::PhysicalKey;

use std::marker::PhantomData;
Expand Down Expand Up @@ -400,7 +399,7 @@ impl<Doc: DocumentLike, Rend: DocumentRenderer> View<Doc, Rend> {
if changed {
let cursor = self.doc.as_ref().get_cursor();
if let Some(cursor) = cursor {
self.window.set_cursor(stylo_to_winit::cursor(cursor));
self.window.set_cursor(cursor);
self.request_redraw();
}
}
Expand Down Expand Up @@ -449,3 +448,17 @@ impl<Doc: DocumentLike, Rend: DocumentRenderer> View<Doc, Rend> {
}
}
}

fn theme_to_color_scheme(theme: Theme) -> ColorScheme {
match theme {
Theme::Light => ColorScheme::Light,
Theme::Dark => ColorScheme::Dark,
}
}

fn color_scheme_to_theme(scheme: ColorScheme) -> Theme {
match scheme {
ColorScheme::Light => Theme::Light,
ColorScheme::Dark => Theme::Dark,
}
}

0 comments on commit 23fa191

Please sign in to comment.