-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Atom & Sequence Document Split (#557)
Co-authored-by: bigfood <[email protected]>
- Loading branch information
Showing
91 changed files
with
2,735 additions
and
1,827 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::sync::{Arc, Mutex, MutexGuard}; | ||
|
||
#[allow(dead_code)] | ||
pub struct Atom<Atom: Atomic> { | ||
atom: Mutex<Atom>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl<TAtom: Atomic> Atom<TAtom> { | ||
pub fn get(&self) -> MutexGuard<TAtom> { | ||
self.atom.lock().unwrap() | ||
} | ||
|
||
pub fn update<F>(&self, f: F) | ||
where | ||
F: FnOnce(&mut TAtom), | ||
{ | ||
let mut atom = self.atom.lock().unwrap(); | ||
f(&mut atom); | ||
atom.on_update(); | ||
namui::event::send(namui::NamuiEvent::NoUpdateJustRender); | ||
} | ||
|
||
pub(crate) fn new(atom: TAtom) -> Self { | ||
Self { | ||
atom: Mutex::new(atom), | ||
} | ||
} | ||
} | ||
|
||
pub struct OptionAtom<Atom: Atomic> { | ||
inner: Mutex<Option<Arc<Atom>>>, | ||
} | ||
|
||
impl<TAtom: Atomic> OptionAtom<TAtom> { | ||
pub fn get_unwrap(&self) -> Arc<TAtom> { | ||
let inner = self.inner.lock().unwrap(); | ||
inner.as_ref().unwrap().clone() | ||
} | ||
|
||
pub fn set(&self, atom: TAtom) { | ||
let mut inner = self.inner.lock().unwrap(); | ||
*inner = Some(Arc::new(atom)); | ||
namui::event::send(namui::NamuiEvent::NoUpdateJustRender); | ||
} | ||
|
||
pub fn update<F>(&self, f: F) | ||
where | ||
F: FnOnce(&mut TAtom), | ||
{ | ||
let mut inner = self.inner.lock().unwrap(); | ||
let Some(atom) = inner.as_mut() else { | ||
return; | ||
}; | ||
let atom = Arc::get_mut(atom).unwrap(); | ||
f(&mut *atom); | ||
atom.on_update(); | ||
namui::event::send(namui::NamuiEvent::NoUpdateJustRender); | ||
} | ||
|
||
pub(crate) const fn new() -> Self { | ||
Self { | ||
inner: Mutex::new(None), | ||
} | ||
} | ||
} | ||
|
||
pub trait Atomic { | ||
fn on_update(&self); | ||
} | ||
|
||
impl<T: Atomic> Atomic for Option<T> { | ||
fn on_update(&self) { | ||
if let Some(value) = self { | ||
value.on_update(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::storage::get_project_cg_part_variant_image_url; | ||
use namui::prelude::*; | ||
use rpc::data::*; | ||
|
||
pub struct CgRenderProps { | ||
pub rect: Rect<Px>, | ||
pub project_id: Uuid, | ||
pub cg_id: Uuid, | ||
} | ||
|
||
pub fn render_cg(props: CgRenderProps, screen_cg: &ScreenCg, cg_file: &CgFile) -> RenderingTree { | ||
render(screen_cg.parts.iter().map(|part| { | ||
try_render(|| { | ||
let cg_part = cg_file.parts.iter().find(|part| part.name == part.name)?; | ||
Some(render_cg_part(&props, part, cg_part)) | ||
}) | ||
})) | ||
} | ||
|
||
fn render_cg_part( | ||
props: &CgRenderProps, | ||
part: &rpc::data::ScreenCgPart, | ||
cg_part: &CgPart, | ||
) -> RenderingTree { | ||
match part { | ||
rpc::data::ScreenCgPart::Single { variant_name, .. } => try_render(|| { | ||
let variant_name = variant_name.as_ref()?; | ||
|
||
let variant = cg_part | ||
.variants | ||
.iter() | ||
.find(|variant| &variant.name == variant_name)?; | ||
|
||
Some(render_cg_variant(props, variant)) | ||
}), | ||
rpc::data::ScreenCgPart::Multi { variant_names, .. } => render( | ||
cg_part | ||
.variants | ||
.iter() | ||
.filter(|variant| variant_names.contains(&variant.name)) | ||
.map(|variant| render_cg_variant(props, variant)), | ||
), | ||
rpc::data::ScreenCgPart::AlwaysOn { .. } => render( | ||
cg_part | ||
.variants | ||
.iter() | ||
.map(|variant| render_cg_variant(props, variant)), | ||
), | ||
} | ||
} | ||
|
||
fn render_cg_variant(props: &CgRenderProps, variant: &rpc::data::CgPartVariant) -> RenderingTree { | ||
try_render(|| { | ||
let rect = Rect::Xywh { | ||
x: props.rect.x() + props.rect.width() * variant.rect.x(), | ||
y: props.rect.y() + props.rect.height() * variant.rect.y(), | ||
width: props.rect.width() * variant.rect.width(), | ||
height: props.rect.height() * variant.rect.height(), | ||
}; | ||
|
||
let url = get_project_cg_part_variant_image_url(props.project_id, props.cg_id, variant.id) | ||
.unwrap(); | ||
|
||
let image = namui::image::try_load_url(&url)?; | ||
Some(namui::image(ImageParam { | ||
rect, | ||
source: ImageSource::Image(image), | ||
style: ImageStyle { | ||
fit: ImageFit::Fill, | ||
paint_builder: None, | ||
}, | ||
})) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ pub enum Item { | |
text: String, | ||
on_click: ClosurePtr<(), ()>, | ||
}, | ||
|
||
#[allow(dead_code)] | ||
Divider, | ||
} | ||
|
||
|
Oops, something went wrong.