-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
813 lines (687 loc) · 29.5 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
///
/// Custom DatePicker Widget
///
mod date_picker {
use iced_graphics::{Backend, Defaults, Primitive, Renderer};
use iced_native::{
layout, mouse, Background, Color, Element, Hasher, Layout, Length,
Point, Size, Widget, Event, Clipboard, Rectangle, Font, HorizontalAlignment,
VerticalAlignment
};
use chrono::prelude::*;
const DAYS_EACH_MONTH: [u32; 13] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29];
const WEEK_DAYS: [&str; 7] = ["Mon","Tue","Web","Thu","Fri","Sat","Sun"];
const HIGH_LIGHT_COLOR: Color = Color{r: 118.0/255.0, g: 179.0/225.0, b: 175.0/255.0, a: 1.0};
const BACKGROUND_COLOR: Color = Color{r: 241.0/255.0, g: 241.0/255.0, b: 241.0/255.0, a: 1.0};
const BORDER_COLOR: Color = Color{r: 0.0/255.0, g: 0.0/255.0, b: 0.0/255.0, a: 1.0};
const FIRST_TEXT_COLOR: Color = Color{r: 0.0/255.0, g: 0.0/255.0, b: 0.0/255.0, a: 1.0};
const SECOND_TEXT_COLOR: Color = Color{r: 200.0/255.0, g: 200.0/255.0, b: 200.0/255.0, a: 1.0};
/// A field that can select a date
pub struct DatePicker<'a, Message> {
w: u32,
h: u32,
placeholder: String,
value: String,
on_change: Box<dyn Fn(String) -> Message>,
on_focus: Option<Message>,
padding: Option<u16>,
size: Option<u16>,
state: &'a mut State,
}
impl<'a, Message> DatePicker<'a, Message> {
/// create a new [`DatePicker`]
pub fn new<F>(
w: u32,
state: &'a mut State,
placeholder: &str,
value: &str,
on_change: F,
) -> Self
where
F: 'static + Fn(String) -> Message,
{
Self {
w,
h: w,
state,
placeholder: String::from(placeholder),
value: String::from(value),
on_change: Box::new(on_change),
on_focus: None,
padding: None,
size: None
}
}
/// check if the year is leap year.
/// leap year: # of days of Feb = 29
/// not leap year: # of days of Feb = 28
fn check_leap_year(&self, year: i32) -> bool {
if year % 100 == 0 {
if year % 400 == 0 { true } else { false }
} else {
if year % 4 == 0 { true } else { false }
}
}
/// how many days of the current month
fn number_days_month(&self, month: u32, year:i32) -> u32 {
let days = if month == 2 {
if self.check_leap_year(year) {
&DAYS_EACH_MONTH[12]
} else {
&DAYS_EACH_MONTH[(month - 1) as usize]
}
} else {
&DAYS_EACH_MONTH[(month - 1) as usize]
};
*days
}
/// how many days of the last month
fn number_days_last_month(&self, month: u32, year:i32) -> u32 {
let last_month = if month == 1 {
12
} else {
month - 1
};
let new_year = if month == 1 {
year - 1
} else {
year
};
return self.number_days_month(last_month, new_year);
}
/// how many days of the next month
fn number_days_next_month(&self, month: u32, year:i32) -> u32 {
let next_month = if month == 12 {
1
} else {
month + 1
};
let new_year = if month == 12 {
year + 1
} else {
year
};
return self.number_days_month(next_month, new_year);
}
/// format date 2020-06-05
pub fn format_date(&self) -> String{
let dt = Utc.ymd(
self.state.year,
self.state.month,
self.state.day);
dt.format("%Y-%m-%d").to_string()
}
/// set the handler when the [`DatePicker`] is
/// focus
pub fn on_focus(mut self, msg: Message) -> Self {
self.on_focus = Some(msg);
self
}
/// Sets the padding of the [`DatePicker`].
pub fn padding(mut self, padding: u16) -> Self {
self.padding = Some(padding);
self
}
/// Sets the text size of the [`DatePicker`].
pub fn size(mut self, size: u16) -> Self {
self.size = Some(size);
self
}
}
/// The state of a [`DatePicker`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
is_pressed: bool,
pub is_focused: bool,
year: i32,
month: u32,
day: u32,
}
impl State {
/// Creates a new [`State`], representing an unfocus [`DatePicker`]
/// with current date: Year, Month, Day
pub fn new() -> State {
let local: DateTime<Local> = Local::now();
State {
is_pressed: false,
is_focused: false,
year: local.year(),
month: local.month(),
day: local.day(),
}
}
}
impl<'a, Message, B> Widget<Message, Renderer<B>> for DatePicker<'a, Message>
where
B: Backend,
Message: Clone,
{
fn width(&self) -> Length {
Length::from(self.w as u16)
}
fn height(&self) -> Length {
Length::from(self.h as u16)
}
/// Layout function
/// Change when the focus event and unfocus event happen
/// return the frame of the widget
fn layout(
&self,
_renderer: &Renderer<B>,
limits: &layout::Limits,
) -> layout::Node {
let padding = self.padding.unwrap_or(10) as f32;
let text_size = self.size.unwrap_or(20);
let limits = limits
.pad(padding)
.width(Length::Units((self.w as f32 - 2.0 * padding) as u16))
.max_width(self.w)
.height(Length::Units(text_size));
let mut text = layout::Node::new(limits.resolve(Size::ZERO));
text.move_to(Point::new(padding, padding));
let input = layout::Node::with_children(text.size().pad(padding), vec![text]);
let mut calendar = layout::Node::new(Size::new(
self.w as f32,
self.h as f32 / 7.0 * 8.0
));
calendar.move_to(Point::new(0.0, input.bounds().height));
layout::Node::with_children(Size::new(
self.w as f32,
if self.state.is_focused {
input.bounds().height + calendar.bounds().height
} else {
input.bounds().height
}
), if self.state.is_focused {
vec![input, calendar]
} else {
vec![input]
})
}
/// through the hash values of several properties
/// to decide if need to relayout the most bottom view
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
self.w.hash(state);
self.h.hash(state);
self.padding.hash(state);
self.size.hash(state);
self.state.is_focused.hash(state);
}
/// According the bound of the layout node,
/// Draw the view
fn draw(
&self,
_renderer: &mut Renderer<B>,
_defaults: &Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> (Primitive, mouse::Interaction) {
let is_mouse_over = layout.bounds().contains(_cursor_position);
let mut v = Vec::new();
// Draw the most bottom view: Background
{
let bound = layout.bounds();
v.push(Primitive::Quad {
bounds: bound,
background: Background::Color(Color::WHITE),
border_radius: 0,
border_width: 0,
border_color: Color::TRANSPARENT,
});
}
for (i, child) in layout.children().enumerate() {
let bound = child.bounds();
if i == 0 {
// Draw the input view and text view
v.push(Primitive::Quad {
bounds: bound,
background: Background::Color(Color::WHITE),
border_radius: 5,
border_width: 1,
border_color: BORDER_COLOR,
});
for chi in child.children() {
v.push(Primitive::Text {
content: if self.value.is_empty() {
self.placeholder.clone()
} else {
self.value.clone()
},
color: if self.value.is_empty() {
SECOND_TEXT_COLOR
} else {
FIRST_TEXT_COLOR
},
font: Font::default(),
bounds: Rectangle {
y: chi.bounds().center_y(),
width: f32::INFINITY,
..chi.bounds()
},
size: self.size.unwrap_or(20) as f32,
horizontal_alignment: HorizontalAlignment::Left,
vertical_alignment: VerticalAlignment::Center,
});
}
} else if i == 1 {
// Draw the calendar view
let size = bound.width/7.0;
let font1 = 24.0/(400.0/7.0) * size;
let font2 = 36.0/(400.0/7.0) * size;
// Draw a background
v.push(Primitive::Quad {
bounds: bound,
background: Background::Color(Color::WHITE),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
// Draw the weekdays menu
// Mon, Tue, Web, Thu, Fri, Sat, Sun
for weekday in 0..7 {
let b = Rectangle {
x: bound.x + weekday as f32 * size,
y: bound.y,
width: bound.width/7.0,
height: bound.width/7.0,
};
let x = b.center_x();
let y = b.center_y();
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(BACKGROUND_COLOR),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
v.push(Primitive::Text {
content: WEEK_DAYS[weekday].to_string(),
bounds: Rectangle { x, y, ..b },
color: FIRST_TEXT_COLOR,
size: font1,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
})
}
// first day of the current month
let dt = Utc.ymd(self.state.year, self.state.month, 1);
// weekday of the first day
let weekday = dt.weekday().num_days_from_monday();
// # of days of current month
let days = self.number_days_month(self.state.month,
self.state.year);
// # of days of last month
let last_month_days = self.number_days_last_month(
self.state.month, self.state.year);
// Draw the days of last month
for day in 1..weekday + 1 {
let temp = last_month_days - (weekday - day);
let column = day - 1;
let b = Rectangle {
x: bound.x + column as f32 * size,
y: bound.y + size,
width: size,
height: size,
};
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(BACKGROUND_COLOR),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
let x = b.center_x();
let y = b.center_y();
v.push(Primitive::Text {
content: temp.to_string(),
bounds: Rectangle { x, y, ..b},
color: SECOND_TEXT_COLOR,
size: font2,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
})
}
// Draw the days of current month
for day in 1..(days + 1){
let temp = day + weekday - 1;
let row = temp / 7 + 1;
let column = temp % 7;
let color = if self.state.day == day {
HIGH_LIGHT_COLOR
} else {
BACKGROUND_COLOR
};
let b = Rectangle {
x: bound.x + column as f32 * size,
y: bound.y + row as f32 * size,
width: size,
height: size,
};
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(color),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
let x = b.center_x();
let y = b.center_y();
v.push(Primitive::Text {
content: day.to_string(),
bounds: Rectangle { x, y, ..b},
color: FIRST_TEXT_COLOR,
size: font2,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
})
}
// Draw the days of next month
for day in weekday + days..42 {
let temp = day - weekday - days + 1;
let row = day / 7 + 1;
let column = day % 7;
let b = Rectangle {
x: bound.x + column as f32 * size,
y: bound.y + row as f32 * size,
width: size,
height: size,
};
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(BACKGROUND_COLOR),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
let x = b.center_x();
let y = b.center_y();
v.push(Primitive::Text {
content: temp.to_string(),
bounds: Rectangle { x, y, ..b},
color: SECOND_TEXT_COLOR,
size: font2,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
})
}
// Draw the select date label on the bottom
{
let b = Rectangle {
x: bound.x + 2.0 * size,
y: bound.y + bound.height - size,
width: 3.0 * size,
height: size,
};
let x = b.center_x();
let y = b.center_y();
v.push(Primitive::Text {
content: self.format_date(),
bounds: Rectangle { x, y, ..b },
color: FIRST_TEXT_COLOR,
size: font1,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
});
}
// Draw the control button on the bottom
// Pre and Next operation
{
let mut b = Rectangle {
x: bound.x,
y: bound.y + bound.height - size,
width: 2.0 * size,
height: size,
};
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(HIGH_LIGHT_COLOR),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
let mut x = b.center_x();
let mut y = b.center_y();
v.push(Primitive::Text {
content: String::from("Pre"),
bounds: Rectangle { x, y, ..b },
color: FIRST_TEXT_COLOR,
size: font1,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
});
b.x = bound.x + 5.0 * size;
v.push(Primitive::Quad {
bounds: b,
background: Background::Color(HIGH_LIGHT_COLOR),
border_radius: 0,
border_width: 1,
border_color: BORDER_COLOR,
});
x = b.center_x();
y = b.center_y();
v.push(Primitive::Text {
content: String::from("Next"),
bounds: Rectangle {x, y, ..b},
color: FIRST_TEXT_COLOR,
size: font1,
font: Font::default(),
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Center,
});
}
}
}
(
Primitive::Group{
primitives: v
},
if is_mouse_over {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
},
)
}
// listen all the event on the window
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
_renderer: &Renderer<B>,
_clipboard: Option<&dyn Clipboard>,
) {
let size = layout.bounds().width/7.0;
match event {
// listen press event
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
let bounds = layout.bounds();
self.state.is_pressed = bounds.contains(cursor_position);
}
// listen released event
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
let bounds = layout.bounds();
let is_clicked = self.state.is_pressed
&& bounds.contains(cursor_position);
self.state.is_pressed = false;
// if the click position is in the region of the widget
if is_clicked {
// if click in the input area
let input_height = (self.padding.unwrap_or(10) * 2 +
self.size.unwrap_or(20)) as f32;
if cursor_position.y < layout.bounds().y + input_height {
// send message to change the [state.is_focus]
if let Some(on_focus) = self.on_focus.clone() {
messages.push(on_focus);
}
return
}
// if click in the calendar area
let column = ((cursor_position.x - layout.bounds().x)/size).ceil() as i32;
let row = ((cursor_position.y - layout.bounds().y - input_height)/size).ceil() as i32;
let dt = Utc.ymd(self.state.year, self.state.month, 1);
let weekday = dt.weekday().num_days_from_monday() as u32;
// if click in the date area in the calendar
if row > 1 && row < 8{
let days = self.number_days_month(
self.state.month, self.state.year);
let day = ((row - 2) * 7 + column) as u32;
// if click the date in the current month
if day > weekday && day <= days + weekday {
self.state.day = day - weekday;
} else if day <= weekday {
// if click the date in the last month
let last_month_days = self.number_days_last_month(
self.state.month, self.state.year);
if self.state.month == 1 {
self.state.year = self.state.year - 1;
self.state.month = 12;
} else {
self.state.month = self.state.month - 1;
}
self.state.day = last_month_days - (weekday - day);
} else if day > days + weekday {
// if click the date in the next month
if self.state.month == 12 {
self.state.year = self.state.year + 1;
self.state.month = 1;
} else {
self.state.month = self.state.month + 1;
}
self.state.day = day - weekday - days;
}
let message = (self.on_change)(self.format_date());
messages.push(message);
} else if row == 8 {
// if click in the control area in the calendar
if column == 1 || column == 2 {
// if click pre button
let last_month_days = self.number_days_last_month(
self.state.month, self.state.year);
if self.state.month == 1 {
self.state.year = self.state.year - 1;
self.state.month = 12;
} else {
self.state.month = self.state.month - 1;
}
// if last month doesn't have current day
if self.state.day > last_month_days {
self.state.day = last_month_days;
}
} else if column == 6 || column == 7 {
// if click the next button
let next_month_days = self.number_days_next_month(
self.state.month, self.state.year);
if self.state.month == 12 {
self.state.year = self.state.year + 1;
self.state.month = 1;
} else {
self.state.month = self.state.month + 1;
}
// if next month doesn't have current day
if self.state.day > next_month_days {
self.state.day = next_month_days;
}
}
}
} else {
// if click on the area outside of the widget
if let Some(on_focus) = self.on_focus.clone() {
if self.state.is_focused {
messages.push(on_focus);
}
}
return
}
}
_ => {}
}
}
}
impl<'a, Message, B> Into<Element<'a, Message, Renderer<B>>> for DatePicker<'a, Message>
where
B: Backend,
Message: 'a + Clone,
{
fn into(self) -> Element<'a, Message, Renderer<B>> {
Element::new(self)
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
use date_picker::DatePicker;
use iced::{
Align, Column, Container, Element, Length, Sandbox, Settings, Text,
};
pub fn main() {
Example::run(Settings::default())
}
struct Example {
width: u32,
date_picker: date_picker::State,
input_value: String,
}
#[derive(Debug, Clone)]
enum Message {
InputChanged(String),
DatePickerfocus,
}
impl Sandbox for Example {
type Message = Message;
fn new() -> Self {
Example {
width: 320,
date_picker: date_picker::State::new(),
input_value: String::default(),
}
}
fn title(&self) -> String {
String::from("Custom widget - DatePicker")
}
fn update(&mut self, message: Message) {
match message {
Message::InputChanged(value) => {
self.input_value = value;
self.date_picker.is_focused = !self.date_picker.is_focused;
},
Message::DatePickerfocus => self.date_picker.is_focused = !self.date_picker.is_focused,
}
}
fn view(&mut self) -> Element<Message> {
let content = Column::new()
.padding(20)
.spacing(20)
.max_width(500)
.align_items(Align::Center)
.push(DatePicker::new(
self.width,
&mut self.date_picker,
"Choose a date...",
&self.input_value,
Message::InputChanged
).padding(10)
.size(30)
.on_focus(Message::DatePickerfocus))
.push(Text::new(format!("Width: {}", self.width.to_string())));
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.align_y(Align::Start)
.into()
}
}