Skip to content

Commit

Permalink
Some changes on top
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Jul 14, 2021
1 parent 722d841 commit ed8b81b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 165 deletions.
105 changes: 17 additions & 88 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
#[macro_use]
extern crate log;

use bottom::{canvas, data_conversion::*, options::*, *};
use bottom::{
canvas,
drawing::{paint, View},
options::*,
*,
};

use std::{
boxed::Box,
Expand Down Expand Up @@ -139,18 +144,10 @@ fn main() -> Result<()> {
break;
}
handle_force_redraws(&mut app);

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
BottomEvent::MouseInput(event) => {
handle_mouse_event(event, &mut app);
handle_force_redraws(&mut app);

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
}
BottomEvent::Update(data) => {
app.data_collection.eat_data(data);
Expand All @@ -162,97 +159,29 @@ fn main() -> Result<()> {
app.is_force_redraw = true;
}

if !app.is_frozen {
// Convert all data into tui-compliant components

// Network
if app.used_widgets.use_net {
let network_data = convert_network_data_points(
&app.data_collection,
false,
app.app_config_fields.use_basic_mode
|| app.app_config_fields.use_old_network_legend,
&app.app_config_fields.network_scale_type,
&app.app_config_fields.network_unit_type,
app.app_config_fields.network_use_binary_prefix,
);
app.canvas_data.network_data_rx = network_data.rx;
app.canvas_data.network_data_tx = network_data.tx;
app.canvas_data.rx_display = network_data.rx_display;
app.canvas_data.tx_display = network_data.tx_display;
if let Some(total_rx_display) = network_data.total_rx_display {
app.canvas_data.total_rx_display = total_rx_display;
}
if let Some(total_tx_display) = network_data.total_tx_display {
app.canvas_data.total_tx_display = total_tx_display;
}
}

// Disk
if app.used_widgets.use_disk {
app.canvas_data.disk_data = convert_disk_row(&app.data_collection);
}

// Temperatures
if app.used_widgets.use_temp {
app.canvas_data.temp_sensor_data = convert_temp_row(&app);
}

// Memory
if app.used_widgets.use_mem {
app.canvas_data.mem_data =
convert_mem_data_points(&app.data_collection, false);
app.canvas_data.swap_data =
convert_swap_data_points(&app.data_collection, false);
let (memory_labels, swap_labels) =
convert_mem_labels(&app.data_collection);

app.canvas_data.mem_labels = memory_labels;
app.canvas_data.swap_labels = swap_labels;
}

if app.used_widgets.use_cpu {
// CPU

convert_cpu_data_points(
&app.data_collection,
&mut app.canvas_data.cpu_data,
false,
);
app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest;
}

// Processes
if app.used_widgets.use_proc {
update_all_process_lists(&mut app);
}

// Battery
if app.used_widgets.use_battery {
app.canvas_data.battery_data =
convert_battery_harvest(&app.data_collection);
}
}

if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
update_app_data(&mut app);
}
BottomEvent::Clean => {
app.data_collection
.clean_data(constants::STALE_MAX_MILLISECONDS);

continue;
}
BottomEvent::RequestRedraw => {
if try_drawing(&mut terminal, &mut app, &mut painter).is_err() {
break;
}
// FIXME: Add draws back to all required locations!
}
}

// TODO: Create new draw state.
let mut root = View::new(drawing::Axis::Horizontal).into();
if paint(&mut terminal, &mut root).is_err() {
break;
}
}
}

*thread_termination_lock.lock().unwrap() = true;
thread_termination_cvar.notify_all();
thread_termination_cvar.notify_all(); // Tell all threads to stop, even if they are sleeping.
cleanup_terminal(&mut terminal)?;

// ===== End of actual thread creation and loop =====
Expand Down
30 changes: 15 additions & 15 deletions src/canvas/canvas_colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,77 +79,77 @@ impl CanvasColours {
pub fn set_colours_from_palette(&mut self, colours: &ConfigColours) -> anyhow::Result<()> {
if let Some(border_color) = &colours.border_color {
self.set_border_colour(border_color)
.context("Update 'border_color' in your config file..")?;
.context("Update 'border_color' in your config file.")?;
}

if let Some(highlighted_border_color) = &colours.highlighted_border_color {
self.set_highlighted_border_colour(highlighted_border_color)
.context("Update 'highlighted_border_color' in your config file..")?;
.context("Update 'highlighted_border_color' in your config file.")?;
}

if let Some(text_color) = &colours.text_color {
self.set_text_colour(text_color)
.context("Update 'text_color' in your config file..")?;
.context("Update 'text_color' in your config file.")?;
}

if let Some(avg_cpu_color) = &colours.avg_cpu_color {
self.set_avg_cpu_colour(avg_cpu_color)
.context("Update 'avg_cpu_color' in your config file..")?;
.context("Update 'avg_cpu_color' in your config file.")?;
}

if let Some(all_cpu_color) = &colours.all_cpu_color {
self.set_all_cpu_colour(all_cpu_color)
.context("Update 'all_cpu_color' in your config file..")?;
.context("Update 'all_cpu_color' in your config file.")?;
}

if let Some(cpu_core_colors) = &colours.cpu_core_colors {
self.set_cpu_colours(cpu_core_colors)
.context("Update 'cpu_core_colors' in your config file..")?;
.context("Update 'cpu_core_colors' in your config file.")?;
}

if let Some(ram_color) = &colours.ram_color {
self.set_ram_colour(ram_color)
.context("Update 'ram_color' in your config file..")?;
.context("Update 'ram_color' in your config file.")?;
}

if let Some(swap_color) = &colours.swap_color {
self.set_swap_colour(swap_color)
.context("Update 'swap_color' in your config file..")?;
.context("Update 'swap_color' in your config file.")?;
}

if let Some(rx_color) = &colours.rx_color {
self.set_rx_colour(rx_color)
.context("Update 'rx_color' in your config file..")?;
.context("Update 'rx_color' in your config file.")?;
}

if let Some(tx_color) = &colours.tx_color {
self.set_tx_colour(tx_color)
.context("Update 'tx_color' in your config file..")?;
.context("Update 'tx_color' in your config file.")?;
}

if let Some(table_header_color) = &colours.table_header_color {
self.set_table_header_colour(table_header_color)
.context("Update 'table_header_color' in your config file..")?;
.context("Update 'table_header_color' in your config file.")?;
}

if let Some(scroll_entry_text_color) = &colours.selected_text_color {
self.set_scroll_entry_text_color(scroll_entry_text_color)
.context("Update 'selected_text_color' in your config file..")?;
.context("Update 'selected_text_color' in your config file.")?;
}

if let Some(scroll_entry_bg_color) = &colours.selected_bg_color {
self.set_scroll_entry_bg_color(scroll_entry_bg_color)
.context("Update 'selected_bg_color' in your config file..")?;
.context("Update 'selected_bg_color' in your config file.")?;
}

if let Some(widget_title_color) = &colours.widget_title_color {
self.set_widget_title_colour(widget_title_color)
.context("Update 'widget_title_color' in your config file..")?;
.context("Update 'widget_title_color' in your config file.")?;
}

if let Some(graph_color) = &colours.graph_color {
self.set_graph_colour(graph_color)
.context("Update 'graph_color' in your config file..")?;
.context("Update 'graph_color' in your config file.")?;
}

if let Some(high_battery_color) = &colours.high_battery_color {
Expand Down
8 changes: 4 additions & 4 deletions src/drawing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub use hasher::Hasher;

use crate::utils::error;

/// The draw function. Draws the entire app.
/// Goes through a few phases:
/// The paint function. Draws the entire app.
/// Intended to go through a few phases:
///
/// 0. Create our "Element" tree from "Widgets". This is from our given [`Widget`] representation.
/// This should have access to app state, and we can do caching checks here to see if we can avoid computations.
Expand All @@ -26,11 +26,11 @@ use crate::utils::error;
/// This is *sort of* like how Flutter does it. We have our Widget tree, which is the non-instantiated representation
/// of our hierarchy. We then actually instantiate this into Elements. Then, we finally actually lay it out, which
/// would correspond to the RenderObject tree. Then, we draw!
pub fn draw<B: Backend>(
pub fn paint<B: Backend>(
terminal: &mut Terminal<B>, root: &mut Element<'_, B>,
) -> error::Result<()> {
terminal.draw(|ctx| {
// Current implementation does zero caching.
// Current implementation does zero caching. Just blind draws. Sorry~

let layout = root.layout(ctx.size());
root.draw(ctx, &layout);
Expand Down
2 changes: 1 addition & 1 deletion src/drawing/rendering/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait Widget<B: Backend> {
// fn hash(&self, state: &mut Hasher);

/// How the [`Widget`] should handle an event. Defaults to ignoring the event.
fn on_event(&mut self, event: Event) -> EventStatus {
fn on_event(&mut self, _event: Event) -> EventStatus {
EventStatus::Ignored
}

Expand Down
18 changes: 7 additions & 11 deletions src/drawing/widgets/basic/alert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use tui::{backend::Backend, layout::Constraint};
use tui::{
backend::Backend,
layout::{Constraint, Rect},
};

use crate::drawing::{Axis, Button, Element, Event, Text, View, Widget};

Expand Down Expand Up @@ -51,22 +54,15 @@ impl<'a, B: Backend> Widget<B> for Alert<'a, B> {
self.body.draw(ctx, node);
}

fn layout(&self, bounds: tui::layout::Rect) -> crate::drawing::Node {
fn layout(&self, bounds: Rect) -> crate::drawing::Node {
self.body.layout(bounds)
}

fn width(&self) -> tui::layout::Constraint {
fn width(&self) -> Constraint {
self.body.width()
}

fn height(&self) -> tui::layout::Constraint {
fn height(&self) -> Constraint {
self.body.height()
}
fn on_event(&mut self, event: Event) -> crate::drawing::EventStatus {
crate::drawing::EventStatus::Ignored
// match event {
// Event::Mouse(event) => todo!(),
// Event::Keyboard(event) => todo!(),
// }
}
}
1 change: 1 addition & 0 deletions src/drawing/widgets/basic/carousel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<'a, B: Backend> Widget<B> for Carousel<'a, B> {
// Draw arrows

// Now draw the rest of the element...
todo!()
}

fn layout(&self, bounds: tui::layout::Rect) -> crate::drawing::Node {
Expand Down
9 changes: 0 additions & 9 deletions src/drawing/widgets/basic/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ where
fn height(&self) -> tui::layout::Constraint {
self.height
}

fn on_event(&mut self, event: Event) -> crate::drawing::EventStatus {
// Support the enter key and a left click action.
crate::drawing::EventStatus::Ignored
// match event {
// Event::Mouse(event) => todo!(),
// Event::Keyboard(event) => todo!(),
// }
}
}

impl<'a, B: Backend> From<Text<'a>> for Element<'a, B> {
Expand Down
4 changes: 0 additions & 4 deletions src/drawing/widgets/basic/text_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,4 @@ where
fn height(&self) -> Constraint {
self.height
}

fn on_event(&mut self, event: Event) -> EventStatus {
EventStatus::Ignored
}
}
8 changes: 0 additions & 8 deletions src/drawing/widgets/basic/time_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ impl<'a, B: Backend> Widget<B> for TimeGraph<'a> {
fn height(&self) -> Constraint {
self.height
}

fn on_event(&mut self, event: Event) -> EventStatus {
crate::drawing::EventStatus::Ignored
// match event {
// Event::Mouse(event) => todo!(),
// Event::Keyboard(event) => todo!(),
// }
}
}

/// Interpolates between two points. Mainly used to help fill in tui-rs blanks in certain situations.
Expand Down
Loading

0 comments on commit ed8b81b

Please sign in to comment.