33// Iced backend for Plotters
44// Copyright: 2022, Joylei <[email protected] > 55// License: MIT
6+
67extern crate iced;
78extern crate plotters;
89extern crate sysinfo;
910
1011use chrono:: { DateTime , Utc } ;
1112use iced:: {
1213 alignment:: { Horizontal , Vertical } ,
13- canvas:: { Cache , Frame , Geometry } ,
14- executor, scrollable, Alignment , Application , Column , Command , Container , Element , Font ,
15- Length , Row , Scrollable , Settings , Size , Space , Subscription ,
14+ executor,
15+ widget:: {
16+ canvas:: { Cache , Frame , Geometry } ,
17+ Column , Container , Row , Scrollable , Space , Text ,
18+ } ,
19+ Alignment , Application , Command , Element , Font , Length , Settings , Size , Subscription , Theme ,
1620} ;
1721use plotters:: prelude:: ChartBuilder ;
1822use plotters_backend:: DrawingBackend ;
@@ -58,6 +62,7 @@ impl Application for State {
5862 type Message = self :: Message ;
5963 type Executor = executor:: Default ;
6064 type Flags = ( ) ;
65+ type Theme = Theme ;
6166
6267 fn new ( _flags : Self :: Flags ) -> ( Self , Command < Self :: Message > ) {
6368 (
@@ -81,14 +86,14 @@ impl Application for State {
8186 Command :: none ( )
8287 }
8388
84- fn view ( & mut self ) -> Element < ' _ , Self :: Message > {
89+ fn view ( & self ) -> Element < ' _ , Self :: Message > {
8590 let content = Column :: new ( )
8691 . spacing ( 20 )
8792 . align_items ( Alignment :: Start )
8893 . width ( Length :: Fill )
8994 . height ( Length :: Fill )
9095 . push (
91- iced :: Text :: new ( "Iced test chart" )
96+ Text :: new ( "Iced test chart" )
9297 . size ( TITLE_FONT_SIZE )
9398 . font ( FONT_BOLD ) ,
9499 )
@@ -115,7 +120,6 @@ struct SystemChart {
115120 last_sample_time : Instant ,
116121 items_per_row : usize ,
117122 processors : Vec < CpuUsageChart > ,
118- scroll : scrollable:: State ,
119123 chart_height : u16 ,
120124}
121125
@@ -128,7 +132,6 @@ impl Default for SystemChart {
128132 last_sample_time : Instant :: now ( ) ,
129133 items_per_row : 3 ,
130134 processors : Default :: default ( ) ,
131- scroll : Default :: default ( ) ,
132135 chart_height : 300 ,
133136 }
134137 }
@@ -171,20 +174,18 @@ impl SystemChart {
171174 }
172175 }
173176
174- fn view ( & mut self ) -> Element < Message > {
177+ fn view ( & self ) -> Element < Message > {
175178 if !self . is_initialized ( ) {
176- iced :: Text :: new ( "Loading..." )
179+ Text :: new ( "Loading..." )
177180 . horizontal_alignment ( Horizontal :: Center )
178181 . vertical_alignment ( Vertical :: Center )
179182 . into ( )
180183 } else {
181- let mut scroll = Scrollable :: new ( & mut self . scroll )
182- . width ( Length :: Fill )
183- . height ( Length :: Fill ) ;
184+ let mut col = Column :: new ( ) . width ( Length :: Fill ) . height ( Length :: Fill ) ;
184185
185186 let chart_height = self . chart_height ;
186187 let mut idx = 0 ;
187- for chunk in self . processors . chunks_mut ( self . items_per_row ) {
188+ for chunk in self . processors . chunks ( self . items_per_row ) {
188189 let mut row = Row :: new ( )
189190 . spacing ( 15 )
190191 . padding ( 20 )
@@ -199,10 +200,10 @@ impl SystemChart {
199200 row = row. push ( Space :: new ( Length :: Fill , Length :: Fill ) ) ;
200201 idx += 1 ;
201202 }
202- scroll = scroll . push ( row) ;
203+ col = col . push ( row) ;
203204 }
204205
205- scroll . into ( )
206+ Scrollable :: new ( col ) . height ( Length :: Fill ) . into ( )
206207 }
207208 }
208209}
@@ -239,13 +240,13 @@ impl CpuUsageChart {
239240 self . cache . clear ( ) ;
240241 }
241242
242- fn view ( & mut self , idx : usize ) -> Element < Message > {
243+ fn view ( & self , idx : usize ) -> Element < Message > {
243244 Container :: new (
244245 Column :: new ( )
245246 . width ( Length :: Fill )
246247 . height ( Length :: Fill )
247248 . spacing ( 5 )
248- . push ( iced :: Text :: new ( format ! ( "Processor {}" , idx) ) )
249+ . push ( Text :: new ( format ! ( "Processor {}" , idx) ) )
249250 . push (
250251 ChartWidget :: new ( self ) . height ( Length :: Fill ) . resolve_font (
251252 |_, style| match style {
@@ -255,7 +256,6 @@ impl CpuUsageChart {
255256 ) ,
256257 ) ,
257258 )
258- . style ( style:: ChartContainer )
259259 . width ( Length :: Fill )
260260 . height ( Length :: Fill )
261261 . align_x ( Horizontal :: Center )
@@ -265,6 +265,7 @@ impl CpuUsageChart {
265265}
266266
267267impl Chart < Message > for CpuUsageChart {
268+ type State = ( ) ;
268269 // fn update(
269270 // &mut self,
270271 // event: Event,
@@ -280,7 +281,7 @@ impl Chart<Message> for CpuUsageChart {
280281 self . cache . draw ( bounds, draw_fn)
281282 }
282283
283- fn build_chart < DB : DrawingBackend > ( & self , mut chart : ChartBuilder < DB > ) {
284+ fn build_chart < DB : DrawingBackend > ( & self , _state : & Self :: State , mut chart : ChartBuilder < DB > ) {
284285 use plotters:: { prelude:: * , style:: Color } ;
285286
286287 const PLOT_LINE_COLOR : RGBColor = RGBColor ( 0 , 175 , 255 ) ;
@@ -307,14 +308,14 @@ impl Chart<Message> for CpuUsageChart {
307308
308309 chart
309310 . configure_mesh ( )
310- . bold_line_style ( & plotters:: style:: colors:: WHITE . mix ( 0.1 ) )
311- . light_line_style ( & plotters:: style:: colors:: WHITE . mix ( 0.05 ) )
312- . axis_style ( ShapeStyle :: from ( & plotters:: style:: colors:: WHITE . mix ( 0.45 ) ) . stroke_width ( 1 ) )
311+ . bold_line_style ( & plotters:: style:: colors:: BLUE . mix ( 0.1 ) )
312+ . light_line_style ( & plotters:: style:: colors:: BLUE . mix ( 0.05 ) )
313+ . axis_style ( ShapeStyle :: from ( & plotters:: style:: colors:: BLUE . mix ( 0.45 ) ) . stroke_width ( 1 ) )
313314 . y_labels ( 10 )
314315 . y_label_style (
315316 ( "sans-serif" , 15 )
316317 . into_font ( )
317- . color ( & plotters:: style:: colors:: WHITE . mix ( 0.65 ) )
318+ . color ( & plotters:: style:: colors:: BLUE . mix ( 0.65 ) )
318319 . transform ( FontTransform :: Rotate90 ) ,
319320 )
320321 . y_label_formatter ( & |y| format ! ( "{}%" , y) )
@@ -333,18 +334,3 @@ impl Chart<Message> for CpuUsageChart {
333334 . expect ( "failed to draw chart data" ) ;
334335 }
335336}
336-
337- mod style {
338- use iced:: Color ;
339-
340- pub struct ChartContainer ;
341- impl iced:: container:: StyleSheet for ChartContainer {
342- fn style ( & self ) -> iced:: container:: Style {
343- iced:: container:: Style {
344- background : Some ( Color :: BLACK . into ( ) ) ,
345- text_color : Some ( Color :: WHITE ) ,
346- ..Default :: default ( )
347- }
348- }
349- }
350- }
0 commit comments