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
2 changes: 1 addition & 1 deletion examples/win_widget/win_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum CrsWinMsg {
Test(i32, i32),
}

#[extends(Widget)]
#[extends(Widget, Layout(HBox))]
#[win_widget(o2s = "CrsWinMsg", s2o(CrsWinMsg))]
#[run_after]
pub struct MyWinWidget {}
Expand Down
19 changes: 9 additions & 10 deletions tmui/src/animation/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use super::{
AnimationState, Direction,
};
use crate::{
animation::state_holder::ReflectRectHolder, prelude::*, primitive::frame::Frame,
widget::WidgetImpl,
graphics::element::ElementInner, prelude::*, primitive::frame::Frame, widget::WidgetImpl,
};
use std::ptr::NonNull;

Expand Down Expand Up @@ -150,10 +149,10 @@ pub trait Snapshot: WidgetImpl + Animatable {

if let Some(rect_holder) = cast!(self as RectHolder) {
let rect = rect_holder.animated_rect();
self.set_fixed_x(rect.x() as i32);
self.set_fixed_y(rect.y() as i32);
self.set_fixed_width(rect.width() as i32);
self.set_fixed_height(rect.height() as i32);
self.as_element().set_fixed_x(rect.x() as i32);
self.as_element().set_fixed_y(rect.y() as i32);
self.as_element().set_fixed_width(rect.width() as i32);
self.as_element().set_fixed_height(rect.height() as i32);
if self.animation_model().mode() == AnimationMode::Flex {
ApplicationWindow::window_of(self.window_id())
.animation_layout_change(self.as_widget_mut());
Expand All @@ -175,10 +174,10 @@ pub trait Snapshot: WidgetImpl + Animatable {
}

if let Some(origin) = self.animation_model().origin_rect() {
self.set_fixed_x(origin.x());
self.set_fixed_y(origin.y());
self.set_fixed_width(origin.width());
self.set_fixed_height(origin.height());
self.as_element().set_fixed_x(origin.x());
self.as_element().set_fixed_y(origin.y());
self.as_element().set_fixed_width(origin.width());
self.as_element().set_fixed_height(origin.height());
}

if let Some(transparency) = self.animation_model().origin_transparency() {
Expand Down
7 changes: 6 additions & 1 deletion tmui/src/application_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
container::ContainerLayoutEnum,
graphics::{
board::Board,
element::{HierachyZ, RenderOrder, TOP_Z_INDEX},
element::{ElementInner, HierachyZ, RenderOrder, TOP_Z_INDEX},
},
input::{dialog::TyInputDialog, focus_mgr::FocusMgr, ReflectInputEle},
layout::LayoutMgr,
Expand Down Expand Up @@ -741,6 +741,11 @@ impl ApplicationWindow {
self.crs_win_handlers
.iter_mut()
.for_each(|hnd| nonnull_mut!(hnd).handle_inner());

self.widgets.values_mut().for_each(|hnd| {
let widget = nonnull_mut!(hnd);
widget.set_rect_record(widget.rect_f());
});
}

#[inline]
Expand Down
52 changes: 29 additions & 23 deletions tmui/src/font/mgr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use ahash::AHashMap;
use derivative::Derivative;
use lazy_static::lazy_static;
use log::error;
use std::{cell::RefCell, io::Read};
use std::{cell::RefCell, io::Read, sync::Arc};
use tipc::parking_lot::Mutex;
use tlib::{
skia_safe::FontMgr,
typedef::{SkiaFontStyle, SkiaTypeface},
Expand Down Expand Up @@ -40,6 +42,11 @@ thread_local! {
static MGR: RefCell<FontManager> = RefCell::new(FontManager::default());
}

type FontLoaderFn = dyn Fn(&mut FontManager) + 'static + Send + Sync;
lazy_static! {
static ref FONT_LOADER: Arc<Mutex<Option<Box<FontLoaderFn>>>> = Arc::new(Mutex::new(None));
}

impl FontManager {
#[inline]
pub(crate) fn load_fonts() {
Expand All @@ -64,6 +71,10 @@ impl FontManager {

manager.fonts.insert(tf.family_name(), tf);
}

if let Some(f) = FONT_LOADER.lock().as_ref() {
f(&mut manager)
}
})
}
}
Expand All @@ -79,39 +90,34 @@ impl FontManager {
}

#[inline]
pub fn load_file(path: &str) {
pub fn register_font_loader<F: Fn(&mut FontManager) + 'static + Send + Sync>(loader: F) {
*FONT_LOADER.lock() = Some(Box::new(loader));
}

#[inline]
pub fn load_file(&mut self, path: &str) {
let mut file =
std::fs::File::open(path).unwrap_or_else(|_| panic!("Open file `{}` failed.", path));

let mut data = vec![];
file.read_to_end(&mut data)
.unwrap_or_else(|_| panic!("Read file `{}` failed", path));

MGR.with(|mgr| {
let mut manager = mgr.borrow_mut();
let tf = self
.system_mgr
.new_from_data(&data, None)
.unwrap_or_else(|| panic!("Make customize font typeface failed, ttf file: {}.", path));

let tf = manager
.system_mgr
.new_from_data(&data, None)
.unwrap_or_else(|| {
panic!("Make customize font typeface failed, ttf file: {}.", path)
});

manager.fonts.insert(tf.family_name(), tf);
});
self.fonts.insert(tf.family_name(), tf);
}

#[inline]
pub fn load_data(data: &[u8]) {
MGR.with(|mgr| {
let mut manager = mgr.borrow_mut();

let tf = manager
.system_mgr
.new_from_data(data, None)
.unwrap_or_else(|| panic!("Make customize font typeface failed."));
pub fn load_data(&mut self, data: &[u8]) {
let tf = self
.system_mgr
.new_from_data(data, None)
.unwrap_or_else(|| panic!("Make customize font typeface failed."));

manager.fonts.insert(tf.family_name(), tf);
})
self.fonts.insert(tf.family_name(), tf);
}
}
84 changes: 51 additions & 33 deletions tmui/src/graphics/element.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{board::Board, drawing_context::DrawingContext};
use crate::{application_window::window_id, widget::WidgetImpl};
use crate::{application_window::window_id, popup::PopupImpl, widget::WidgetImpl};
use log::error;
use tlib::{
figure::{FRect, Rect},
Expand Down Expand Up @@ -99,18 +99,6 @@ pub trait ElementExt {
/// Get the geometry rect of element which contains element's size and position.
fn rect_f(&self) -> FRect;

/// Set the width of element.
fn set_fixed_width(&mut self, width: i32);

/// Set the height of element.
fn set_fixed_height(&mut self, height: i32);

/// Set the x position of element.
fn set_fixed_x(&mut self, x: i32);

/// Set the y position of element.
fn set_fixed_y(&mut self, y: i32);

/// The element was invalidated or not.
fn invalidate(&self) -> bool;

Expand Down Expand Up @@ -169,26 +157,6 @@ impl<T: ElementImpl> ElementExt for T {
self.element_props().rect
}

#[inline]
fn set_fixed_width(&mut self, width: i32) {
self.element_props_mut().rect.set_width(width as f32)
}

#[inline]
fn set_fixed_height(&mut self, height: i32) {
self.element_props_mut().rect.set_height(height as f32)
}

#[inline]
fn set_fixed_x(&mut self, x: i32) {
self.element_props_mut().rect.set_x(x as f32)
}

#[inline]
fn set_fixed_y(&mut self, y: i32) {
self.element_props_mut().rect.set_y(y as f32)
}

#[inline]
fn invalidate(&self) -> bool {
match self.get_property("invalidate") {
Expand All @@ -211,6 +179,56 @@ impl<T: ElementImpl> ElementExt for T {
}
}

pub(crate) trait ElementInner {
/// Set the width of element.
fn set_fixed_width(&mut self, width: i32);

/// Set the height of element.
fn set_fixed_height(&mut self, height: i32);

/// Set the x position of element.
fn set_fixed_x(&mut self, x: i32);

/// Set the y position of element.
fn set_fixed_y(&mut self, y: i32);
}
macro_rules! element_inner_impl {
() => {
#[inline]
fn set_fixed_width(&mut self, width: i32) {
self.element_props_mut().rect.set_width(width as f32)
}

#[inline]
fn set_fixed_height(&mut self, height: i32) {
self.element_props_mut().rect.set_height(height as f32)
}

#[inline]
fn set_fixed_x(&mut self, x: i32) {
self.element_props_mut().rect.set_x(x as f32)
}

#[inline]
fn set_fixed_y(&mut self, y: i32) {
self.element_props_mut().rect.set_y(y as f32)
}
};
}

impl<T: ElementImpl> ElementInner for T {
element_inner_impl!();
}
impl ElementInner for dyn ElementImpl {
element_inner_impl!();
}
impl ElementInner for dyn WidgetImpl {
element_inner_impl!();
}
impl ElementInner for dyn PopupImpl {
element_inner_impl!();
}

/// Every Element's subclass should impl this trait manually, and implements `on_renderer` function. <br>
/// Each subclass which impl [`WidgetImpl`] will impl this trait automatically.
#[reflect_trait]
Expand Down
1 change: 1 addition & 0 deletions tmui/src/hbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
ContainerLayoutEnum, ContainerScaleCalculate, ScaleStrat, StaticContainerScaleCalculate,
StaticSizeUnifiedAdjust, SCALE_ADAPTION,
},
graphics::element::ElementInner,
layout::LayoutMgr,
prelude::*,
};
Expand Down
1 change: 1 addition & 0 deletions tmui/src/icons/font_icon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
font::FontCalculation,
graphics::element::ElementInner,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
widget::{widget_inner::WidgetInnerExt, WidgetImpl},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/icons/svg_icon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
graphics::element::ElementInner,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
widget::{widget_inner::WidgetInnerExt, WidgetImpl},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/icons/svg_toggle_icon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Read;

use crate::{
graphics::element::ElementInner,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
widget::{widget_inner::WidgetInnerExt, WidgetImpl},
Expand Down
2 changes: 1 addition & 1 deletion tmui/src/input/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{
InputType,
};
use crate::{
graphics::styles::Styles,
graphics::{element::ElementInner, styles::Styles},
input::InputSignals,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/input/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::widget::widget_ext::FocusStrat;
use crate::{
asset::Asset,
font::FontCalculation,
graphics::element::ElementInner,
input::INPUT_DEFAULT_BORDER_COLOR,
input_ele_impl,
prelude::*,
Expand Down
1 change: 1 addition & 0 deletions tmui/src/input/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
system::System,
tlib::object::{ObjectImpl, ObjectSubclass},
widget::{widget_inner::WidgetInnerExt, RegionClear, WidgetImpl},
graphics::element::ElementInner
};
use log::warn;
use std::{
Expand Down
2 changes: 1 addition & 1 deletion tmui/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
graphics::painter::Painter,
graphics::{element::ElementInner, painter::Painter},
layout::ContentAlignment,
prelude::*,
widget::{widget_inner::WidgetInnerExt, WidgetImpl},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
container::{Container, ContainerImpl, ReflectSizeUnifiedAdjust, ScaleMeasure, SpacingSize},
graphics::element::ElementInner,
opti::tracker::Tracker,
prelude::*,
primitive::Message,
Expand Down
1 change: 1 addition & 0 deletions tmui/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
container::{
ContainerLayoutEnum, ContainerScaleCalculate, StaticContainerScaleCalculate, SCALE_DISMISS,
},
graphics::element::ElementInner,
layout::LayoutMgr,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/popup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
graphics::element::ElementInner,
prelude::*,
tlib::object::{ObjectImpl, ObjectSubclass},
widget::WidgetImpl,
Expand Down
4 changes: 1 addition & 3 deletions tmui/src/runtime/wed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
application,
application_window::ApplicationWindow,
graphics::element::HierachyZ,
graphics::element::{ElementInner, HierachyZ},
input::focus_mgr::FocusMgr,
prelude::*,
primitive::{global_watch::GlobalWatchEvent, Message},
Expand All @@ -18,8 +18,6 @@ use tlib::{
values::ToValue,
};

use super::ElementExt;

//////////////////////////////////////////////////////////////////////////////////////////////
/// [`Event`] dispatch
//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions tmui/src/scroll_area.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
application_window::ApplicationWindow,
container::{ContainerLayoutEnum, ContainerScaleCalculate, SCALE_ADAPTION},
graphics::element::ElementInner,
layout::LayoutMgr,
prelude::*,
scroll_bar::{ScrollBar, ScrollBarPosition},
Expand Down
1 change: 1 addition & 0 deletions tmui/src/split_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
ContainerLayoutEnum, ContainerScaleCalculate, ReflectSizeUnifiedAdjust,
StaticContainerScaleCalculate, SCALE_DISMISS,
},
graphics::element::ElementInner,
layout::LayoutMgr,
prelude::*,
tlib::{
Expand Down
1 change: 1 addition & 0 deletions tmui/src/vbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
ContainerLayoutEnum, ContainerScaleCalculate, ScaleStrat, StaticContainerScaleCalculate,
StaticSizeUnifiedAdjust, SCALE_ADAPTION,
},
graphics::element::ElementInner,
layout::LayoutMgr,
prelude::*,
};
Expand Down
Loading