Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
remove piet dependency
This is working on mac only at the moment. It also needs rework for cursors and IME hit testing.
  • Loading branch information
dfrg committed Jun 27, 2022
commit e9d8def957f3e8067eb75a0b8a8077ccf070bc05
50 changes: 34 additions & 16 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rustdoc-args = ["--cfg", "docsrs"]
default-target = "x86_64-pc-windows-msvc"

[features]
default = ["gtk"]
default = ["gtk", "raw-window-handle"]
gtk = ["gdk-sys", "glib-sys", "gtk-sys", "gtk-rs"]
x11 = [
"ashpd",
Expand Down Expand Up @@ -44,26 +44,44 @@ raw-win-handle = ["raw-window-handle"]

# passing on all the image features. AVIF is not supported because it does not
# support decoding, and that's all we use `Image` for.
image_png = ["piet-common/image_png"]
jpeg = ["piet-common/jpeg"]
jpeg_rayon = ["piet-common/jpeg_rayon"]
gif = ["piet-common/gif"]
bmp = ["piet-common/bmp"]
ico = ["piet-common/ico"]
tiff = ["piet-common/tiff"]
webp = ["piet-common/webp"]
pnm = ["piet-common/pnm"]
dds = ["piet-common/dds"]
tga = ["piet-common/tga"]
farbfeld = ["piet-common/farbfeld"]
dxt = ["piet-common/dxt"]
hdr = ["piet-common/hdr"]
# image_png = ["piet-common/image_png"]
# jpeg = ["piet-common/jpeg"]
# jpeg_rayon = ["piet-common/jpeg_rayon"]
# gif = ["piet-common/gif"]
# bmp = ["piet-common/bmp"]
# ico = ["piet-common/ico"]
# tiff = ["piet-common/tiff"]
# webp = ["piet-common/webp"]
# pnm = ["piet-common/pnm"]
# dds = ["piet-common/dds"]
# tga = ["piet-common/tga"]
# farbfeld = ["piet-common/farbfeld"]
# dxt = ["piet-common/dxt"]
# hdr = ["piet-common/hdr"]
bmp = []
dds = []
dxt = []
farbfeld = []
gif = []
jpeg = []
png = []
ico = []
tiff = []
webp = []
tga = []
hdr = []
image_png = []
jpeg_rayon = []
pnm = []



serde = ["kurbo/serde"]

[dependencies]
# NOTE: When changing the piet or kurbo versions, ensure that
# the kurbo version included in piet is compatible with the kurbo version specified here.
piet-common = "=0.5.0"
# piet-common = "=0.5.0"
kurbo = "0.8.2"

tracing = "0.1.22"
Expand Down
12 changes: 7 additions & 5 deletions druid-shell/src/backend/mac/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,13 @@ pub extern "C" fn character_index_for_point(
_: Sel,
point: NSPoint,
) -> NSUInteger {
with_edit_lock_from_window(this, true, |edit_lock| {
let hit_test = edit_lock.hit_test_point(Point::new(point.x, point.y));
hit_test.idx as NSUInteger
})
.unwrap_or(0)
// TODO: figure out how to do text hit testing without piet
// with_edit_lock_from_window(this, true, |edit_lock| {
// let hit_test = edit_lock.hit_test_point(Point::new(point.x, point.y));
// hit_test.idx as NSUInteger
// })
// .unwrap_or(0)
0
}

pub extern "C" fn first_rect_for_character_range(
Expand Down
29 changes: 1 addition & 28 deletions druid-shell/src/backend/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use tracing::{debug, error, info};
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};

use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
use crate::piet::{Piet, PietText, RenderContext};

use super::appkit::{
NSRunLoopCommonModes, NSTrackingArea, NSTrackingAreaOptions, NSView as NSViewExt,
Expand Down Expand Up @@ -175,7 +174,6 @@ struct ViewState {
// Tracks whether we have already received the mouseExited event
mouse_left: bool,
keyboard_state: KeyboardState,
text: PietText,
active_text_input: Option<TextFieldToken>,
parent: Option<crate::WindowHandle>,
}
Expand Down Expand Up @@ -556,7 +554,6 @@ fn make_view(handler: Box<dyn WinHandler>) -> (id, Weak<Mutex<Vec<IdleKind>>>) {
focus_click: false,
mouse_left: true,
keyboard_state,
text: PietText::new_with_unique_state(),
active_text_input: None,
parent: None,
};
Expand Down Expand Up @@ -848,13 +845,6 @@ extern "C" fn view_will_draw(this: &mut Object, _: Sel) {

extern "C" fn draw_rect(this: &mut Object, _: Sel, dirtyRect: NSRect) {
unsafe {
let context: id = msg_send![class![NSGraphicsContext], currentContext];
//FIXME: when core_graphics is at 0.20, we should be able to use
//core_graphics::sys::CGContextRef as our pointer type.
let cgcontext_ptr: *mut <CGContextRef as ForeignTypeRef>::CType =
msg_send![context, CGContext];
let cgcontext_ref = CGContextRef::from_ptr_mut(cgcontext_ptr);

// FIXME: use the actual invalid region instead of just this bounding box.
// https://developer.apple.com/documentation/appkit/nsview/1483772-getrectsbeingdrawn?language=objc
let rect = Rect::from_origin_size(
Expand All @@ -865,12 +855,8 @@ extern "C" fn draw_rect(this: &mut Object, _: Sel, dirtyRect: NSRect) {

let view_state: *mut c_void = *this.get_ivar("viewState");
let view_state = &mut *(view_state as *mut ViewState);
let mut piet_ctx = Piet::new_y_down(cgcontext_ref, Some(view_state.text.clone()));

(*view_state).handler.paint(&mut piet_ctx, &invalid);
if let Err(e) = piet_ctx.finish() {
error!("{}", e)
}
(*view_state).handler.paint(&invalid);

let superclass = msg_send![this, superclass];
let () = msg_send![super(this, superclass), drawRect: dirtyRect];
Expand Down Expand Up @@ -1106,19 +1092,6 @@ impl WindowHandle {
token
}

pub fn text(&self) -> PietText {
let view = self.nsview.load();
unsafe {
if let Some(view) = (*view).as_ref() {
let state: *mut c_void = *view.get_ivar("viewState");
(*(state as *mut ViewState)).text.clone()
} else {
// this codepath should only happen during tests in druid, when view is nil
PietText::new_with_unique_state()
}
}
}

pub fn add_text_field(&self) -> TextFieldToken {
TextFieldToken::next()
}
Expand Down
1 change: 0 additions & 1 deletion druid-shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ extern crate gtk_rs as gtk;
pub use image;

pub use kurbo;
pub use piet_common as piet;

// Reexport the version of `raw_window_handle` we are using.
#[cfg(feature = "raw-win-handle")]
Expand Down
8 changes: 3 additions & 5 deletions druid-shell/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use crate::backend;
use crate::kurbo::{Point, Vec2};
use crate::piet::ImageBuf;
use crate::Modifiers;

/// Information about the mouse event.
Expand Down Expand Up @@ -270,8 +269,8 @@ pub enum Cursor {
/// A platform-independent description of a custom cursor.
#[derive(Clone)]
pub struct CursorDesc {
#[allow(dead_code)] // Not yet used on all platforms.
pub(crate) image: ImageBuf,
// #[allow(dead_code)] // Not yet used on all platforms.
// pub(crate) image: ImageBuf,
#[allow(dead_code)] // Not yet used on all platforms.
pub(crate) hot: Point,
}
Expand All @@ -283,9 +282,8 @@ impl CursorDesc {
/// `(0, 0)` at the top left. The hot spot is the logical position of the mouse cursor within
/// the image. For example, if the image is a picture of a arrow, the hot spot might be the
/// coordinates of the arrow's tip.
pub fn new(image: ImageBuf, hot: impl Into<Point>) -> CursorDesc {
pub fn new(hot: impl Into<Point>) -> CursorDesc {
CursorDesc {
image,
hot: hot.into(),
}
}
Expand Down
3 changes: 1 addition & 2 deletions druid-shell/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@

use crate::keyboard::{KbKey, KeyEvent};
use crate::kurbo::{Point, Rect};
use crate::piet::HitTestPoint;
use crate::window::{TextFieldToken, WinHandler};
use std::borrow::Cow;
use std::ops::Range;
Expand Down Expand Up @@ -419,7 +418,7 @@ pub trait InputHandler {
fn replace_range(&mut self, range: Range<usize>, text: &str);

/// Given a `Point`, determine the corresponding text position.
fn hit_test_point(&self, point: Point) -> HitTestPoint;
// fn hit_test_point(&self, point: Point) -> HitTestPoint;

/// Returns the range, in UTF-8 code units, of the line (soft- or hard-wrapped)
/// containing the byte specified by `index`.
Expand Down
9 changes: 2 additions & 7 deletions druid-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::mouse::{Cursor, CursorDesc, MouseEvent};
use crate::region::Region;
use crate::scale::Scale;
use crate::text::{Event, InputHandler};
use piet_common::PietText;

#[cfg(feature = "raw-win-handle")]
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

Expand Down Expand Up @@ -311,11 +311,6 @@ impl WindowHandle {
self.0.set_menu(menu.into_inner())
}

/// Get access to a type that can perform text layout.
pub fn text(&self) -> PietText {
self.0.text()
}

/// Register a new text input receiver for this window.
///
/// This method should be called any time a new editable text field is
Expand Down Expand Up @@ -557,7 +552,7 @@ pub trait WinHandler {
/// Request the handler to paint the window contents. `invalid` is the region in [display
/// points](crate::Scale) that needs to be repainted; painting outside the invalid region will
/// have no effect.
fn paint(&mut self, piet: &mut piet_common::Piet, invalid: &Region);
fn paint(&mut self, invalid: &Region);

/// Called when the resources need to be rebuilt.
///
Expand Down
16 changes: 8 additions & 8 deletions druid/src/text/input_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,14 +877,14 @@ impl<T: TextStorage + EditableText> InputHandler for EditSessionHandle<T> {
self.inner.borrow_mut().external_text_change = Some(self.text.clone());
}

fn hit_test_point(&self, point: Point) -> crate::piet::HitTestPoint {
self.inner
.borrow()
.layout
.layout()
.map(|layout| layout.hit_test_point(point))
.unwrap_or_default()
}
// fn hit_test_point(&self, point: Point) -> crate::piet::HitTestPoint {
// self.inner
// .borrow()
// .layout
// .layout()
// .map(|layout| layout.hit_test_point(point))
// .unwrap_or_default()
// }

fn line_range(&self, index: usize, _affinity: druid_shell::text::Affinity) -> Range<usize> {
let inner = self.inner.borrow();
Expand Down