mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 06:41:14 +00:00

Unfortunately the text property was not included in the property dependency chain for the cached Font. This is fixed by delaying that with a getter function, but in the mid term we should be able to remove this again once femtovg learns lazy font resolution.
174 lines
5.2 KiB
Rust
174 lines
5.2 KiB
Rust
/* LICENSE BEGIN
|
|
This file is part of the SixtyFPS Project -- https://sixtyfps.io
|
|
Copyright (c) 2021 Olivier Goffart <olivier.goffart@sixtyfps.io>
|
|
Copyright (c) 2021 Simon Hausmann <simon.hausmann@sixtyfps.io>
|
|
|
|
SPDX-License-Identifier: GPL-3.0-only
|
|
This file is also available under commercial licensing terms.
|
|
Please contact info@sixtyfps.io for more information.
|
|
LICENSE END */
|
|
/*!
|
|
|
|
*NOTE*: This library is an internal crate for the [SixtyFPS project](https://sixtyfps.io).
|
|
This crate should not be used directly by application using SixtyFPS.
|
|
You should use the `sixtyfps` crate instead.
|
|
|
|
*/
|
|
#![doc(html_logo_url = "https://sixtyfps.io/resources/logo.drawio.svg")]
|
|
|
|
use image::GenericImageView;
|
|
use sixtyfps_corelib::component::ComponentRc;
|
|
use sixtyfps_corelib::graphics::{Image, Point, Size};
|
|
use sixtyfps_corelib::slice::Slice;
|
|
use sixtyfps_corelib::window::{PlatformWindow, Window};
|
|
use sixtyfps_corelib::{ImageInner, SharedString};
|
|
use std::path::Path;
|
|
use std::pin::Pin;
|
|
use std::rc::Rc;
|
|
use std::sync::Mutex;
|
|
|
|
#[derive(Default)]
|
|
pub struct TestingBackend {
|
|
clipboard: Mutex<Option<String>>,
|
|
}
|
|
|
|
impl sixtyfps_corelib::backend::Backend for TestingBackend {
|
|
fn create_window(&'static self) -> Rc<Window> {
|
|
Window::new(|_| Rc::new(TestingWindow::default()))
|
|
}
|
|
|
|
fn run_event_loop(&'static self, _behavior: sixtyfps_corelib::backend::EventLoopQuitBehavior) {
|
|
unimplemented!("running an event loop with the testing backend");
|
|
}
|
|
|
|
fn quit_event_loop(&'static self) {}
|
|
|
|
fn register_font_from_memory(
|
|
&'static self,
|
|
_data: &[u8],
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
Ok(())
|
|
}
|
|
|
|
fn register_font_from_path(
|
|
&'static self,
|
|
_path: &std::path::Path,
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
Ok(())
|
|
}
|
|
|
|
fn set_clipboard_text(&'static self, text: String) {
|
|
*self.clipboard.lock().unwrap() = Some(text);
|
|
}
|
|
|
|
fn clipboard_text(&'static self) -> Option<String> {
|
|
self.clipboard.lock().unwrap().clone()
|
|
}
|
|
|
|
fn post_event(&'static self, _event: Box<dyn FnOnce() + Send>) {
|
|
unimplemented!("event with the testing backend");
|
|
}
|
|
|
|
fn image_size(&'static self, image: &Image) -> Size {
|
|
let inner: &ImageInner = image.into();
|
|
match &inner {
|
|
ImageInner::None => Default::default(),
|
|
ImageInner::EmbeddedImage(buffer) => {
|
|
Size::new(buffer.width() as _, buffer.height() as _)
|
|
}
|
|
ImageInner::AbsoluteFilePath(path) => image::open(Path::new(path.as_str()))
|
|
.map(|img| {
|
|
let dim = img.dimensions();
|
|
Size::new(dim.0 as _, dim.1 as _)
|
|
})
|
|
.unwrap_or_default(),
|
|
ImageInner::EmbeddedData { data, format } => image::load_from_memory_with_format(
|
|
data.as_slice(),
|
|
image::ImageFormat::from_extension(std::str::from_utf8(format.as_slice()).unwrap())
|
|
.unwrap(),
|
|
)
|
|
.map(|img| {
|
|
let dim = img.dimensions();
|
|
Size::new(dim.0 as _, dim.1 as _)
|
|
})
|
|
.unwrap_or_default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct TestingWindow {}
|
|
|
|
impl Default for TestingWindow {
|
|
fn default() -> Self {
|
|
Self {}
|
|
}
|
|
}
|
|
impl PlatformWindow for TestingWindow {
|
|
fn show(self: Rc<Self>) {
|
|
unimplemented!("showing a testing window")
|
|
}
|
|
|
|
fn hide(self: Rc<Self>) {}
|
|
|
|
fn request_redraw(&self) {}
|
|
|
|
fn free_graphics_resources<'a>(
|
|
&self,
|
|
_items: &Slice<'a, Pin<sixtyfps_corelib::items::ItemRef<'a>>>,
|
|
) {
|
|
}
|
|
|
|
fn show_popup(&self, _popup: &ComponentRc, _position: sixtyfps_corelib::graphics::Point) {
|
|
todo!()
|
|
}
|
|
|
|
fn request_window_properties_update(&self) {}
|
|
|
|
fn apply_window_properties(&self, _window_item: Pin<&sixtyfps_corelib::items::WindowItem>) {
|
|
todo!()
|
|
}
|
|
|
|
fn apply_geometry_constraint(
|
|
&self,
|
|
_constraints_horizontal: sixtyfps_corelib::layout::LayoutInfo,
|
|
_constraints_vertical: sixtyfps_corelib::layout::LayoutInfo,
|
|
) {
|
|
}
|
|
|
|
fn text_size(
|
|
&self,
|
|
_item_graphics_cache: &sixtyfps_corelib::item_rendering::CachedRenderingData,
|
|
_unresolved_font_request_getter: &dyn Fn() -> sixtyfps_corelib::graphics::FontRequest,
|
|
text_getter: &dyn Fn() -> SharedString,
|
|
_max_width: Option<f32>,
|
|
) -> Size {
|
|
Size::new(text_getter().len() as f32 * 10., 10.)
|
|
}
|
|
|
|
fn text_input_byte_offset_for_position(
|
|
&self,
|
|
_text_input: Pin<&sixtyfps_corelib::items::TextInput>,
|
|
_pos: Point,
|
|
) -> usize {
|
|
0
|
|
}
|
|
|
|
fn text_input_position_for_byte_offset(
|
|
&self,
|
|
_text_input: Pin<&sixtyfps_corelib::items::TextInput>,
|
|
_byte_offset: usize,
|
|
) -> Point {
|
|
Default::default()
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Initialize the testing backend.
|
|
/// Must be called before any call that would otherwise initialize the rendering backend.
|
|
/// Calling it when the rendering backend is already initialized will have no effects
|
|
pub fn init() {
|
|
sixtyfps_corelib::backend::instance_or_init(|| Box::new(TestingBackend::default()));
|
|
}
|