Skip to content

Commit

Permalink
Sender に send するところを Context 内になるべく閉じるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
mitoma committed Jan 19, 2025
1 parent 3e3dacc commit 047e089
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 111 deletions.
92 changes: 89 additions & 3 deletions font_rasterizer/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::{mpsc::Sender, Arc};

use font_collector::FontRepository;
use log::warn;
use stroke_parser::Action;
use winit::dpi::PhysicalSize;

Expand All @@ -9,17 +10,102 @@ use crate::{
glyph_vertex_buffer::Direction,
};

pub struct Senders {
ui_string_sender: Sender<String>,
action_queue_sender: Sender<Action>,
post_action_queue_sender: Sender<Action>,
}

impl Senders {
pub fn new(
ui_string_sender: Sender<String>,
action_queue_sender: Sender<Action>,
post_action_queue_sender: Sender<Action>,
) -> Self {
Self {
ui_string_sender,
action_queue_sender,
post_action_queue_sender,
}
}
}

pub struct StateContext {
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub char_width_calcurator: Arc<CharWidthCalculator>,
pub color_theme: ColorTheme,
pub window_size: WindowSize,
pub ui_string_sender: Sender<String>,
pub action_queue_sender: Sender<Action>,
pub post_action_queue_sender: Sender<Action>,
pub global_direction: Direction,
pub font_repository: FontRepository,
senders: Senders,
}

impl StateContext {
// TODO: 引数が多いのはまぁ微妙ではあるが、一旦許容している
#[allow(clippy::too_many_arguments)]
#[inline]
pub fn new(
device: wgpu::Device,
queue: wgpu::Queue,
char_width_calcurator: Arc<CharWidthCalculator>,
color_theme: ColorTheme,
window_size: WindowSize,
global_direction: Direction,
font_repository: FontRepository,
senders: Senders,
) -> Self {
Self {
device,
queue,
char_width_calcurator,
color_theme,
window_size,
global_direction,
font_repository,
senders,
}
}

#[inline]
pub fn register_string(&self, value: String) {
match self.senders.ui_string_sender.send(value) {
Ok(_) => {}
Err(err) => {
warn!("Failed to send string: {}", err)
}
}
}

#[inline]
pub fn register_action(&self, action: Action) {
match self.senders.action_queue_sender.send(action) {
Ok(_) => {}
Err(err) => {
warn!("Failed to send action: {}", err)
}
}
}

#[inline]
pub fn register_post_action(&self, action: Action) {
match self.senders.post_action_queue_sender.send(action) {
Ok(_) => {}
Err(err) => {
warn!("Failed to send post action: {}", err)
}
}
}

#[inline]
pub fn action_sender(&self) -> Sender<Action> {
self.senders.action_queue_sender.clone()
}

#[inline]
pub fn post_action_sender(&self) -> Sender<Action> {
self.senders.post_action_queue_sender.clone()
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion font_rasterizer_example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl SimpleStateCallback for SingleCharCallback {
instance.push(value);
self.glyphs.push(instance);
let chars = vec!['あ'].into_iter().collect();
context.ui_string_sender.send(chars).unwrap();
context.register_string(chars);
}

fn update(&mut self, context: &StateContext) {
Expand Down
32 changes: 9 additions & 23 deletions kashikishi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl ActionProcessor for SystemCommandPalette {
_ => None,
};
let modal = command_palette_select(context, narrow);
context.ui_string_sender.send(modal.to_string()).unwrap();
context.register_string(modal.to_string());
world.add_next(Box::new(modal));
world.re_layout();
let adjustment = if context.global_direction == Direction::Horizontal {
Expand Down Expand Up @@ -236,16 +236,10 @@ impl SimpleStateCallback for KashikishiCallback {
chars.insert(caret_char(text_buffer::caret::CaretType::Mark));
// IME のグリフを追加する
chars.extend(ime_chars());
context
.ui_string_sender
.send(chars.into_iter().collect::<String>())
.unwrap();
context.register_string(chars.into_iter().collect::<String>());

// カメラを初期化する
context
.post_action_queue_sender
.send(Action::new_command("world", "fit-by-direction"))
.unwrap();
context.register_post_action(Action::new_command("world", "fit-by-direction"));
}

fn resize(&mut self, window_size: WindowSize) {
Expand Down Expand Up @@ -304,36 +298,28 @@ impl SimpleStateCallback for KashikishiCallback {
if let Some(world) = world {
self.world.graceful_exit();
self.world = world;
context.register_string(self.world.world_chars().into_iter().collect());
context
.ui_string_sender
.send(self.world.world_chars().into_iter().collect())
.unwrap();
context
.post_action_queue_sender
.send(Action::new_command("world", "fit-by-direction"))
.unwrap();
.register_post_action(Action::new_command("world", "fit-by-direction"));
}
InputResult::InputConsumed
}
_ => {
let (result, chars) = self
.world
.apply_action(context, Action::Command(category, name, argument));
context
.ui_string_sender
.send(chars.into_iter().collect())
.unwrap();
context.register_string(chars.into_iter().collect());
result
}
},
Action::Keytype(c) => {
context.ui_string_sender.send(c.to_string()).unwrap();
context.register_string(c.to_string());
let action = EditorOperation::InsertChar(c);
self.world.get_mut().editor_operation(&action);
InputResult::InputConsumed
}
Action::ImeInput(value) => {
context.ui_string_sender.send(value.clone()).unwrap();
context.register_string(value.clone());
self.ime
.apply_ime_event(&Action::ImeInput(value.clone()), context);
self.world
Expand All @@ -342,7 +328,7 @@ impl SimpleStateCallback for KashikishiCallback {
InputResult::InputConsumed
}
Action::ImePreedit(value, position) => {
context.ui_string_sender.send(value.clone()).unwrap();
context.register_string(value.clone());
self.ime
.apply_ime_event(&Action::ImePreedit(value, position), context);
InputResult::InputConsumed
Expand Down
5 changes: 1 addition & 4 deletions kashikishi/src/world/categorized_memos_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ impl ModalWorld for CategorizedMemosWorld {
}
self.memos
.add_memo(Some(&category), self.world.current_string());
context
.action_queue_sender
.send(Action::new_command("world", "remove-current"))
.unwrap();
context.register_action(Action::new_command("world", "remove-current"));
}
_ => { /* noop */ }
}
Expand Down
2 changes: 1 addition & 1 deletion rokid_3dof/examples/rokid_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl SimpleStateCallback for SingleCharCallback {
let mut instance = GlyphInstances::new('あ', &context.device);
instance.push(value);
self.glyphs.push(instance);
context.ui_string_sender.send("あ".to_string()).unwrap();
context.register_string("あ".to_string());
}

fn update(&mut self, context: &StateContext) {
Expand Down
27 changes: 9 additions & 18 deletions sample_codes/showcase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,11 @@ impl SingleCharCallback {

impl SimpleStateCallback for SingleCharCallback {
fn init(&mut self, context: &StateContext) {
set_action_sender(context.action_queue_sender.clone());

context
.ui_string_sender
.send(caret_char(text_buffer::caret::CaretType::Primary).to_string())
.unwrap();
context
.ui_string_sender
.send(caret_char(text_buffer::caret::CaretType::Mark).to_string())
.unwrap();
context
.ui_string_sender
.send(self.world.chars().into_iter().collect::<String>())
.unwrap();
set_action_sender(context.action_sender());

context.register_string(caret_char(text_buffer::caret::CaretType::Primary).to_string());
context.register_string(caret_char(text_buffer::caret::CaretType::Mark).to_string());
context.register_string(self.world.chars().into_iter().collect::<String>());
[
Action::new_command_with_argument("system", "change-theme", "light"),
Action::new_command("edit", "buffer-head"),
Expand All @@ -178,7 +169,7 @@ impl SimpleStateCallback for SingleCharCallback {
]
.into_iter()
.for_each(|action| {
context.action_queue_sender.send(action).unwrap();
context.register_action(action);
});
}

Expand Down Expand Up @@ -215,21 +206,21 @@ impl SimpleStateCallback for SingleCharCallback {
_ => InputResult::Noop,
},
Action::Keytype(c) => {
context.ui_string_sender.send(c.to_string()).unwrap();
context.register_string(c.to_string());
let action = EditorOperation::InsertChar(c);
self.world.editor_operation(&action);
InputResult::InputConsumed
}
Action::ImeInput(value) => {
context.ui_string_sender.send(value.clone()).unwrap();
context.register_string(value.clone());
self.ime
.apply_ime_event(&Action::ImeInput(value.clone()), context);
self.world
.editor_operation(&EditorOperation::InsertString(value));
InputResult::InputConsumed
}
Action::ImePreedit(value, position) => {
context.ui_string_sender.send(value.clone()).unwrap();
context.register_string(value.clone());
self.ime
.apply_ime_event(&Action::ImePreedit(value, position), context);
InputResult::InputConsumed
Expand Down
13 changes: 5 additions & 8 deletions ui_support/examples/save_apng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl SingleCharCallback {

impl SimpleStateCallback for SingleCharCallback {
fn init(&mut self, context: &StateContext) {
context.ui_string_sender.send("あ".to_string()).unwrap();
context.register_string("あ".to_string());
let value = InstanceAttributes::new(
(0.0, 0.0, 0.0).into(),
cgmath::Quaternion::from_axis_angle(cgmath::Vector3::unit_z(), cgmath::Deg(0.0)),
Expand All @@ -131,13 +131,10 @@ impl SimpleStateCallback for SingleCharCallback {
let mut instances = GlyphInstances::new('あ', &context.device);
instances.push(value);
self.glyphs.push(instances);
context
.post_action_queue_sender
.send(stroke_parser::Action::new_command(
"system",
"change-background-image",
))
.unwrap();
context.register_post_action(stroke_parser::Action::new_command(
"system",
"change-background-image",
));
debug!("init!");
}

Expand Down
23 changes: 7 additions & 16 deletions ui_support/examples/save_apng2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,19 @@ impl SingleCharCallback {
impl SimpleStateCallback for SingleCharCallback {
fn init(&mut self, context: &StateContext) {
self.world.add(Box::new(TextEdit::default()));
context
.ui_string_sender
.send("エディタの文字をアニメーションGifにほげ".to_string())
.unwrap();
context.register_string("エディタの文字をアニメーションGifにほげ".to_string());
self.world.editor_operation(&EditorOperation::InsertString(
"エディタの文字をアニメーションGifに".to_string(),
));
self.world
.model_operation(&ModelOperation::ChangeDirection(None));
self.world.look_at(0, CameraAdjustment::FitBoth);
context
.post_action_queue_sender
.send(Action::new_command("world", "reset-zoom"))
.unwrap();
context
.post_action_queue_sender
.send(stroke_parser::Action::new_command_with_argument(
"system",
"change-background-image",
"kashikishi/asset/image/wallpaper.jpg",
))
.unwrap();
context.register_post_action(Action::new_command("world", "reset-zoom"));
context.register_post_action(stroke_parser::Action::new_command_with_argument(
"system",
"change-background-image",
"kashikishi/asset/image/wallpaper.jpg",
));
self.world.editor_operation(&EditorOperation::InsertEnter);
self.world
.editor_operation(&EditorOperation::InsertString("ほげほげ".to_string()));
Expand Down
23 changes: 7 additions & 16 deletions ui_support/examples/save_gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,19 @@ impl SingleCharCallback {
impl SimpleStateCallback for SingleCharCallback {
fn init(&mut self, context: &StateContext) {
self.world.add(Box::new(TextEdit::default()));
context
.ui_string_sender
.send("エディタの文字をアニメーションGifにほげ".to_string())
.unwrap();
context.register_string("エディタの文字をアニメーションGifにほげ".to_string());
self.world.editor_operation(&EditorOperation::InsertString(
"エディタの文字をアニメーションGifに".to_string(),
));
self.world
.model_operation(&ModelOperation::ChangeDirection(None));
self.world.look_at(0, CameraAdjustment::FitBoth);
context
.post_action_queue_sender
.send(Action::new_command("world", "reset-zoom"))
.unwrap();
context
.post_action_queue_sender
.send(stroke_parser::Action::new_command_with_argument(
"system",
"change-background-image",
"kashikishi/asset/image/wallpaper.jpg",
))
.unwrap();
context.register_post_action(Action::new_command("world", "reset-zoom"));
context.register_post_action(Action::new_command_with_argument(
"system",
"change-background-image",
"kashikishi/asset/image/wallpaper.jpg",
));
self.world.editor_operation(&EditorOperation::InsertEnter);
self.world
.editor_operation(&EditorOperation::InsertString("ほげほげ".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion ui_support/examples/setup_glyphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl SimpleStateCallback for SingleCharCallback {
.for_each(|c| {
let start = Instant::now();
println!("c len: {}", c.len());
context.ui_string_sender.send(c).unwrap();
context.register_string(c);
let end = Instant::now();
println!("init: {:?}", end - start);
});
Expand Down
2 changes: 1 addition & 1 deletion ui_support/examples/support_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl SimpleStateCallback for SingleCharCallback {
let mut instance = GlyphInstances::new('あ', &context.device);
instance.push(value);
self.glyphs.push(instance);
context.ui_string_sender.send("あ".to_string()).unwrap();
context.register_string("あ".to_string());
}

fn update(&mut self, context: &StateContext) {
Expand Down
Loading

0 comments on commit 047e089

Please sign in to comment.