| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2026 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use async_trait::async_trait;
- use parking_lot::Mutex as SyncMutex;
- use rand::{rngs::OsRng, Rng};
- use std::sync::Arc;
- use tracing::instrument;
- use crate::{
- gfx::{gfxtag, DrawCall, DrawInstruction, Rectangle, RenderApi},
- prop::{
- BatchGuardPtr, PropertyAtomicGuard, PropertyBool, PropertyColor, PropertyFloat32,
- PropertyRect, PropertyStr, PropertyUint32, Role,
- },
- scene::{Pimpl, SceneNodeWeak},
- text2::{self, TEXT_CTX},
- util::i18n::I18nBabelFish,
- ExecutorPtr,
- };
- use super::{DrawUpdate, OnModify, UIObject};
- macro_rules! t { ($($arg:tt)*) => { trace!(target: "ui::text", $($arg)*); } }
- pub type TextPtr = Arc<Text>;
- pub struct Text {
- node: SceneNodeWeak,
- render_api: RenderApi,
- i18n_fish: I18nBabelFish,
- tasks: SyncMutex<Vec<smol::Task<()>>>,
- dc_key: u64,
- rect: PropertyRect,
- z_index: PropertyUint32,
- priority: PropertyUint32,
- text: PropertyStr,
- font_size: PropertyFloat32,
- text_color: PropertyColor,
- lineheight: PropertyFloat32,
- use_i18n: PropertyBool,
- debug: PropertyBool,
- window_scale: PropertyFloat32,
- parent_rect: SyncMutex<Option<Rectangle>>,
- }
- impl Text {
- pub async fn new(
- node: SceneNodeWeak,
- window_scale: PropertyFloat32,
- render_api: RenderApi,
- i18n_fish: I18nBabelFish,
- ) -> Pimpl {
- let node_ref = &node.upgrade().unwrap();
- let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap();
- let z_index = PropertyUint32::wrap(node_ref, Role::Internal, "z_index", 0).unwrap();
- let priority = PropertyUint32::wrap(node_ref, Role::Internal, "priority", 0).unwrap();
- let text = PropertyStr::wrap(node_ref, Role::Internal, "text", 0).unwrap();
- let font_size = PropertyFloat32::wrap(node_ref, Role::Internal, "font_size", 0).unwrap();
- let text_color = PropertyColor::wrap(node_ref, Role::Internal, "text_color").unwrap();
- let lineheight = PropertyFloat32::wrap(node_ref, Role::Internal, "lineheight", 0).unwrap();
- let use_i18n = PropertyBool::wrap(node_ref, Role::Internal, "use_i18n", 0).unwrap();
- let debug = PropertyBool::wrap(node_ref, Role::Internal, "debug", 0).unwrap();
- let self_ = Arc::new(Self {
- node,
- render_api,
- i18n_fish,
- tasks: SyncMutex::new(vec![]),
- dc_key: OsRng.gen(),
- rect,
- z_index,
- priority,
- text,
- font_size,
- text_color,
- lineheight,
- use_i18n,
- debug,
- window_scale,
- parent_rect: SyncMutex::new(None),
- });
- Pimpl::Text(self_)
- }
- async fn regen_mesh(&self) -> Vec<DrawInstruction> {
- let text = self.text.get();
- let font_size = self.font_size.get();
- let lineheight = self.lineheight.get();
- let text_color = self.text_color.get();
- let window_scale = self.window_scale.get();
- let text = if self.use_i18n.get() {
- if let Some(trans) = self.i18n_fish.tr(&text) {
- //t!("Translate '{text}' to '{trans}'");
- trans
- } else {
- format!("tr err: {}", text)
- }
- } else {
- text
- };
- let layout = {
- let mut txt_ctx = TEXT_CTX.get().await;
- txt_ctx.make_layout(&text, text_color, font_size, lineheight, window_scale, None, &[])
- };
- let mut debug_opts = text2::DebugRenderOptions::OFF;
- if self.debug.get() {
- debug_opts |= text2::DebugRenderOptions::BASELINE;
- }
- text2::render_layout_with_opts(&layout, debug_opts, &self.render_api, gfxtag!("text"))
- }
- #[instrument(target = "ui::text")]
- async fn redraw(self: Arc<Self>, batch: BatchGuardPtr) {
- let Some(parent_rect) = self.parent_rect.lock().clone() else { return };
- let atom = &mut batch.spawn();
- let Some(draw_update) = self.get_draw_calls(atom, parent_rect).await else {
- error!(target: "ui::text", "Text failed to draw");
- return
- };
- self.render_api.replace_draw_calls(batch.id, draw_update.draw_calls);
- }
- async fn get_draw_calls(
- &self,
- atom: &mut PropertyAtomicGuard,
- parent_rect: Rectangle,
- ) -> Option<DrawUpdate> {
- self.rect.eval(atom, &parent_rect).ok()?;
- let rect = self.rect.get();
- let mut instrs = vec![DrawInstruction::Move(rect.pos())];
- instrs.append(&mut self.regen_mesh().await);
- Some(DrawUpdate {
- key: self.dc_key,
- draw_calls: vec![(
- self.dc_key,
- DrawCall::new(instrs, vec![], self.z_index.get(), "text"),
- )],
- })
- }
- }
- #[async_trait]
- impl UIObject for Text {
- fn priority(&self) -> u32 {
- self.priority.get()
- }
- async fn start(self: Arc<Self>, ex: ExecutorPtr) {
- let me = Arc::downgrade(&self);
- let mut on_modify = OnModify::new(ex, self.node.clone(), me.clone());
- on_modify.when_change(self.rect.prop(), Self::redraw);
- on_modify.when_change(self.z_index.prop(), Self::redraw);
- on_modify.when_change(self.text.prop(), Self::redraw);
- on_modify.when_change(self.font_size.prop(), Self::redraw);
- on_modify.when_change(self.text_color.prop(), Self::redraw);
- on_modify.when_change(self.debug.prop(), Self::redraw);
- *self.tasks.lock() = on_modify.tasks;
- }
- fn stop(&self) {
- self.tasks.lock().clear();
- *self.parent_rect.lock() = None;
- }
- #[instrument(target = "ui::text")]
- async fn draw(
- &self,
- parent_rect: Rectangle,
- atom: &mut PropertyAtomicGuard,
- ) -> Option<DrawUpdate> {
- *self.parent_rect.lock() = Some(parent_rect);
- self.get_draw_calls(atom, parent_rect).await
- }
- fn set_i18n(&self, i18n_fish: &I18nBabelFish) {
- self.i18n_fish.set(i18n_fish);
- }
- }
- impl Drop for Text {
- fn drop(&mut self) {
- let atom = self.render_api.make_guard(gfxtag!("Text::drop"));
- self.render_api.replace_draw_calls(atom.batch_id, vec![(self.dc_key, Default::default())]);
- }
- }
- impl std::fmt::Debug for Text {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{:?}", self.node.upgrade().unwrap())
- }
- }
|