Skip to content

Commit

Permalink
shell3: Simplify FontSystem use, port code to Shell3
Browse files Browse the repository at this point in the history
  • Loading branch information
pragmatrix committed Jun 22, 2024
1 parent cd5a6d6 commit 2795351
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 117 deletions.
76 changes: 56 additions & 20 deletions examples/code/examples/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ use ide::{
};
use load_cargo::{LoadCargoConfig, ProcMacroServerChoice};
use project_model::CargoConfig;
use shared::{application, code_viewer};
use shared::{
application2::{Application2, UpdateResponse},
code_viewer,
};
use syntax::{AstNode, SyntaxKind, WalkEvent};
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use vfs::VfsPath;
use winit::event_loop::EventLoop;
use winit::dpi::LogicalSize;

use crate::code_viewer::TextAttribute;
use massive_geometry::{Camera, Color, SizeI};
use massive_scene::PositionedShape;
use massive_shapes::TextWeight;
use massive_shell::Shell;

use crate::code_viewer::TextAttribute;
use massive_shell::{shell3, ApplicationContext3};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -52,11 +56,10 @@ async fn main() -> Result<()> {
// .with(chrome_layer)
.init();

let progress = |p: String| {
let mut handle = io::stdout().lock();
let _ = writeln!(handle, "{}", p.as_str());
};
shell3::run(application).await
}

async fn application(mut ctx: ApplicationContext3) -> Result<()> {
// let root_path = env::current_dir().unwrap().join(Path::new("Cargo.toml"));
let root_path = env::current_dir()
.unwrap()
Expand All @@ -67,7 +70,7 @@ async fn main() -> Result<()> {
let example_dir = root_path
.parent()
.unwrap()
.join(Path::new("shell/examples/code"));
.join(Path::new("examples/code/examples"));

// FontSystem

Expand Down Expand Up @@ -95,13 +98,18 @@ async fn main() -> Result<()> {
prefill_caches: false,
};

let file_to_show = example_dir.join("main.rs");
let file_to_show = example_dir.join("code.rs");
// let file_to_show = example_dir.join("test.rs.demo");

println!("Looking for {}", file_to_show.display());

let progress_writer = |p: String| {
let mut handle = io::stdout().lock();
let _ = writeln!(handle, "{}", p.as_str());
};

let (db, vfs, _proc_macro_server) =
load_cargo::load_workspace_at(&root_path, &cargo_config, &load_config, &progress)?;
load_cargo::load_workspace_at(&root_path, &cargo_config, &load_config, &progress_writer)?;

println!("db: {db:?}");
println!("vfs: {vfs:?}");
Expand Down Expand Up @@ -251,8 +259,7 @@ async fn main() -> Result<()> {

// Window

let event_loop = EventLoop::new()?;
let window = application::create_window(&event_loop, None)?;
let window = ctx.new_window(LogicalSize::new(1024, 800), None)?;
let initial_size = window.inner_size();

// Camera
Expand All @@ -265,14 +272,43 @@ async fn main() -> Result<()> {

// Application

let application =
application::Application::new(camera, glyph_runs, SizeI::new(1280, height as u64));

// Shell
let mut application = Application2::new(SizeI::new(1280, height as u64));

let font_system = Arc::new(Mutex::new(font_system));
let mut shell = Shell::new(&window, initial_size, font_system.clone()).await?;
shell.run(event_loop, &window, application).await

let (mut renderer, mut director) = window
.new_renderer(font_system, camera, initial_size)
.await?;

let mut current_matrix = application.matrix();
let matrix = director.cast(current_matrix);

let _positioned_shapes: Vec<_> = glyph_runs
.into_iter()
.map(|run| director.cast(PositionedShape::new(matrix.clone(), run)))
.collect();

director.action()?;

loop {
let window_event = ctx.wait_for_event(&mut renderer).await?;

info!("Window Event: {window_event:?}");

match application.update(window_event) {
UpdateResponse::Exit => return Ok(()),
UpdateResponse::Continue => {}
}

// DI: This check has to be done in the renderer and the renderer has to decide when it
// needs to redraw.
let new_matrix = application.matrix();
if new_matrix != current_matrix {
matrix.update(new_matrix);
current_matrix = new_matrix;
director.action()?;
}
}
}

fn attribute(tag: HlTag, mods: HlMods) -> (Color, TextWeight) {
Expand Down
17 changes: 11 additions & 6 deletions examples/hello/examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::rc::Rc;
use std::{
rc::Rc,
sync::{Arc, Mutex},
};

use anyhow::Result;
use cosmic_text as text;
Expand All @@ -19,24 +22,26 @@ use massive_shell::{shell3, ApplicationContext3};
async fn main() -> Result<()> {
env_logger::init();

let font_system = FontSystem::new();

shell3::run(font_system, application).await
shell3::run(application).await
}

async fn application(mut ctx: ApplicationContext3) -> Result<()> {
let font_system = Arc::new(Mutex::new(FontSystem::new()));

let fovy: f64 = 45.0;
let camera_distance = 1.0 / (fovy / 2.0).to_radians().tan();
let mut camera = Camera::new((0.0, 0.0, camera_distance), (0.0, 0.0, 0.0));

// camera.eye = Point3::new(0.8999999999999992, 0.0, 0.11421356237309382);

let hello_world = "Hello, world!";
let shapes = render(&mut ctx.font_system().lock().unwrap(), hello_world);
let shapes = render(&mut font_system.lock().unwrap(), hello_world);

let window = ctx.new_window(LogicalSize::new(1280, 800), None)?;

let (mut renderer, mut director) = window.new_renderer(camera, window.inner_size()).await?;
let (mut renderer, mut director) = window
.new_renderer(font_system, camera, window.inner_size())
.await?;

let _positioned_shapes = legacy::into_positioned_shapes(&mut director, shapes);
director.action()?;
Expand Down
16 changes: 9 additions & 7 deletions examples/markdown/examples/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub fn main() -> Result<()> {
}

async fn markdown() -> Result<()> {
shell3::run(application).await
}

async fn application(mut ctx: ApplicationContext3) -> Result<()> {
let font_system = {
// In wasm the system locale can't be acquired. `sys_locale::get_locale()`
const DEFAULT_LOCALE: &str = "en-US";
Expand All @@ -48,12 +52,6 @@ async fn markdown() -> Result<()> {
FontSystem::new_with_locale_and_db(DEFAULT_LOCALE.into(), font_db)
};

// Run

shell3::run(font_system, application).await
}

async fn application(mut ctx: ApplicationContext3) -> Result<()> {
let scale_factor = ctx
.primary_monitor()
.map(|m| m.scale_factor())
Expand All @@ -72,7 +70,11 @@ async fn application(mut ctx: ApplicationContext3) -> Result<()> {

let window = ctx.new_window(initial_size, Some(CANVAS_ID))?;
let (mut renderer, mut director) = window
.new_renderer(camera, initial_size.to_physical(scale_factor))
.new_renderer(
Arc::new(Mutex::new(font_system)),
camera,
initial_size.to_physical(scale_factor),
)
.await?;

let markdown = include_str!("replicator.org.md");
Expand Down
2 changes: 1 addition & 1 deletion shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use massive_shapes::Shape;

pub mod shell3;

pub use shell3::{ApplicationContext3, Shell3, ShellWindow, WindowRenderer};
pub use shell3::{ApplicationContext3, ShellWindow, WindowRenderer};

pub trait Application {
fn update(&mut self, window_event: WindowEvent);
Expand Down
Loading

0 comments on commit 2795351

Please sign in to comment.