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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Dialog boxes
//! 
//! KAS dialog boxes are pre-configured windows, usually allowing some
//! customisation.

use std::fmt::{self, Debug};

use crate::callback::Condition;
use crate::control::{button, TextButton};
use crate::macros::Widget;
use crate::event::{Action, GuiResponse};
use crate::{Class, CoreData, TkWidget, Widget, Window};


/// An action for use with `MessageBox::new`.
pub fn action_close() -> impl Fn() -> GuiResponse {
    || GuiResponse::Close
}

/// A message box.
#[widget(class = Class::Window)]
#[derive(Clone, Widget)]
pub struct MessageBox<M: Debug + 'static, H: 'static> {
    #[core] core: CoreData,
    message: M,
    button: TextButton<H>,
}

impl<M: Debug, R, H: Fn() -> R> MessageBox<M, H> {
    // TODO: action parameter shouldn't be necessary, but we need it because
    // H must be derived from function input somehow, not merely unspecified
    // Once existential types are available, H parameter will not be needed.
    pub fn new(message: M, action: H) -> Self {
        MessageBox{
            core: Default::default(),
            message,
            button: button::ok(action)
        }
    }
}

// manual impl required because derive requires `H: Debug`
impl<M: Debug, H> Debug for MessageBox<M, H> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MessageBox {{ core: {:?}, message: {:?}, button: {:?} }}",
            self.core, self.message, self.button)
    }
}

impl<M: Debug, H> Window for MessageBox<M, H> {
    fn as_widget(&self) -> &Widget { self }
    fn as_widget_mut(&mut self) -> &mut Widget { self }
    
    #[cfg(feature = "layout")]
    fn configure_widgets(&mut self, _tk: &TkWidget) {
        unimplemented!()
    }
    
    #[cfg(feature = "layout")]
    fn resize(&mut self, _tk: &TkWidget, _size: Coord) {
        unimplemented!()
    }
    
    fn handle_action(&mut self, _tk: &TkWidget, _action: Action, _num: u32) -> GuiResponse
    {
        unimplemented!()
    }
    
    // doesn't support callbacks, so doesn't need to do anything here
    fn callbacks(&self) -> Vec<(usize, Condition)> { Vec::new() }
    fn trigger_callback(&mut self, _index: usize, _tk: &TkWidget) {}
    fn on_start(&mut self, _tk: &TkWidget) {}
}