Skip to content

Commit

Permalink
consider blocks as part of tree
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Aug 5, 2024
1 parent 1fb27e2 commit d696803
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 94 deletions.
4 changes: 3 additions & 1 deletion frontends/rioterm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
}

self.set_current(tab_index);
self.current_route = self.contexts[self.current_index].route_id;
}

#[inline]
Expand Down Expand Up @@ -448,6 +449,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
}

self.set_current(self.contexts.len() - 1);
self.current_route = self.contexts[self.current_index].route_id;
}

#[inline]
Expand Down Expand Up @@ -566,7 +568,6 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
pub fn set_current(&mut self, context_id: usize) {
if context_id < self.contexts.len() {
self.current_index = context_id;
self.current_route = self.contexts[self.current_index].route_id;
}
}

Expand All @@ -587,6 +588,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {

self.titles.titles.remove(&index_to_remove);
self.contexts.remove(index_to_remove);
self.current_route = self.contexts[self.current_index].route_id;
}

#[inline]
Expand Down
2 changes: 0 additions & 2 deletions frontends/rioterm/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ impl Sequencer {
working_dir_overwrite,
)) => {
if let Some(route) = self.router.routes.get(&window_id) {
route.redraw();

// This case happens only for native tabs
// every time that a new tab is created through context
// it also reaches for the foreground process path if
Expand Down
54 changes: 5 additions & 49 deletions sugarloaf/src/sugarloaf/compositors/elementary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use crate::components::text::glyph::{FontId, OwnedSection};

use crate::components::text::glyph::OwnedSection;
use crate::sugarloaf::graphics;
use crate::sugarloaf::tree::SugarTree;
use crate::sugarloaf::{PxScale, Rect, SugarText};
use rustc_hash::FxHashMap;

#[derive(Copy, Clone, PartialEq)]
pub struct CachedSugar {
font_id: FontId,
char_width: f32,
px_scale: Option<PxScale>,
}

#[allow(unused)]
struct GraphicRect {
Expand All @@ -31,68 +22,37 @@ struct GraphicRect {

#[derive(Default)]
pub struct Elementary {
sugar_cache: FxHashMap<char, CachedSugar>,
pub rects: Vec<Rect>,
pub blocks_rects: Vec<Rect>,
pub sections: Vec<OwnedSection>,
pub blocks_sections: Vec<OwnedSection>,
pub should_resize: bool,
text_y: f32,
current_row: u16,
graphic_rects: FxHashMap<crate::SugarGraphicId, GraphicRect>,
}

impl Elementary {
#[inline]
pub fn rects(&mut self) -> &Vec<Rect> {
self.rects.extend(&self.blocks_rects);
&self.rects
}

#[inline]
pub fn clean_blocks(&mut self) {
self.blocks_sections.clear();
self.blocks_rects.clear();
}

#[inline]
pub fn blocks_are_empty(&self) -> bool {
self.blocks_sections.is_empty() && self.blocks_rects.is_empty()
}

#[inline]
pub fn set_should_resize(&mut self) {
self.should_resize = true;
}

#[inline]
pub fn extend_block_rects(&mut self, rects: &Vec<Rect>) {
self.blocks_rects.extend(rects);
}

#[inline]
pub fn reset(&mut self) {
// Clean font cache per instance
self.sugar_cache.clear();
self.clean();
}

#[inline]
pub fn clean(&mut self) {
self.rects.clear();
self.sections.clear();
self.graphic_rects.clear();
self.current_row = 0;
self.text_y = 0.0;
self.should_resize = false;
self.rects.clear();
}

#[inline]
pub fn create_section_from_text(
&mut self,
sugar_text: &SugarText,
tree: &SugarTree,
) -> &OwnedSection {
) -> OwnedSection {
let text = crate::components::text::OwnedText {
text: sugar_text.content.to_owned(),
scale: PxScale::from(sugar_text.font_size * tree.layout.dimensions.scale),
Expand All @@ -113,18 +73,14 @@ impl Elementary {
.h_align(crate::components::text::glyph::HorizontalAlign::Left)
};

let section = crate::components::text::OwnedSection {
crate::components::text::OwnedSection {
screen_position: (
sugar_text.position.0 * tree.layout.dimensions.scale,
sugar_text.position.1 * tree.layout.dimensions.scale,
),
bounds: (tree.layout.width, tree.layout.height),
text: vec![text],
layout,
};

self.blocks_sections.push(section);

&self.blocks_sections[self.blocks_sections.len() - 1]
}
}
}
50 changes: 14 additions & 36 deletions sugarloaf/src/sugarloaf/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl SugarState {

#[inline]
pub fn reset_compositor(&mut self) {
self.compositors.elementary.reset();
self.compositors.elementary.clean();
self.compositors.advanced.reset();
}

Expand All @@ -145,10 +145,6 @@ impl SugarState {

advance_brush.prepare(context, self);

for section in &self.compositors.elementary.blocks_sections {
elementary_brush.queue(section);
}

if self.compositors.elementary.should_resize {
rect_brush.resize(context);
}
Expand All @@ -158,29 +154,20 @@ impl SugarState {
// ...
// If current tree has blocks and compositor has empty blocks
// It means that's either the first render or blocks were erased on compute_diff() step
if !self.current.blocks.is_empty()
&& self.compositors.elementary.blocks_are_empty()
{
for block in &self.current.blocks {
if let Some(text) = &block.text {
elementary_brush.queue(
self.compositors
.elementary
.create_section_from_text(text, &self.current),
);
}

if !block.rects.is_empty() {
self.compositors.elementary.extend_block_rects(&block.rects);
}
for block in &self.current.blocks {
if let Some(text) = &block.text {
elementary_brush.queue(
&self
.compositors
.elementary
.create_section_from_text(text, &self.current),
);
}
}

// Add block rects to main rects
self.compositors
.elementary
.rects
.extend(&self.compositors.elementary.blocks_rects);
if !block.rects.is_empty() {
self.compositors.elementary.rects.extend(&block.rects);
}
}

true
}
Expand Down Expand Up @@ -252,27 +239,22 @@ impl SugarState {
}

let mut should_update = false;
let mut should_clean_blocks = false;
let mut should_resize = false;
let mut should_compute_dimensions = false;

self.latest_change =
self.current
.calculate_diff(&self.next, false, self.is_dirty);

match &self.latest_change {
SugarTreeDiff::Equal => {
// Do nothing
}
SugarTreeDiff::LayoutIsDifferent => {
should_update = true;
should_compute_dimensions = true;
should_clean_blocks = true;
should_resize = true;
}
SugarTreeDiff::BlocksAreDifferent => {
should_clean_blocks = true;
should_update = true;
}
SugarTreeDiff::LineQuantity(_) => {
should_update = true;
should_compute_dimensions = true;
Expand All @@ -299,10 +281,6 @@ impl SugarState {
self.compositors.advanced.update_layout(&self.current);
}

if should_clean_blocks {
self.compositors.elementary.clean_blocks();
}

if should_resize {
self.compositors.elementary.set_should_resize();
}
Expand Down
7 changes: 1 addition & 6 deletions sugarloaf/src/sugarloaf/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub enum Diff {
pub enum SugarTreeDiff {
Equal,
Different,
BlocksAreDifferent,
LineQuantity(i32),
LayoutIsDifferent,
Changes(Vec<Diff>),
Expand All @@ -60,11 +59,7 @@ impl SugarTree {
return SugarTreeDiff::LayoutIsDifferent;
}

if self.blocks != next.blocks {
return SugarTreeDiff::BlocksAreDifferent;
}

if is_dirty {
if is_dirty || self.blocks != next.blocks {
return SugarTreeDiff::Different;
}

Expand Down

0 comments on commit d696803

Please sign in to comment.