Blame view

build6/epsilon-master/escher/src/modal_view_controller.cpp 5.19 KB
6663b6c9   adorian   projet complet av...
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
  #include <escher/modal_view_controller.h>
  #include <escher/app.h>
  #include <assert.h>
  
  ModalViewController::ContentView::ContentView() :
    View(),
    m_regularView(nullptr),
    m_currentModalView(nullptr),
    m_isDisplayingModal(false),
    m_verticalAlignment(0.0f),
    m_horizontalAlignment(0.0f),
    m_topMargin(0),
    m_leftMargin(0),
    m_bottomMargin(0),
    m_rightMargin(0)
  {
  }
  
  void ModalViewController::ContentView::setMainView(View * regularView) {
    if (m_regularView != regularView) {
      m_regularView = regularView;
    }
  }
  
  int ModalViewController::ContentView::numberOfSubviews() const {
    return 1 + m_isDisplayingModal;
  }
  
  View * ModalViewController::ContentView::subviewAtIndex(int index) {
    switch (index) {
      case 0:
        return m_regularView;
      case 1:
        if (numberOfSubviews() == 2) {
          return m_currentModalView;
        } else {
          assert(false);
          return nullptr;
        }
      default:
        assert(false);
        return nullptr;
    }
  }
  
  KDRect ModalViewController::ContentView::modalViewFrame() const {
    KDSize modalSize = m_isDisplayingModal ? m_currentModalView->minimalSizeForOptimalDisplay() : KDSize(0,0);
    KDCoordinate modalHeight = modalSize.height();
    modalHeight = modalHeight == 0 ? bounds().height()-m_topMargin-m_bottomMargin : modalHeight;
    KDCoordinate modalWidth = modalSize.width();
    modalWidth = modalWidth == 0 ? bounds().width()-m_leftMargin-m_rightMargin : modalWidth;
    KDRect modalViewFrame(m_leftMargin + m_horizontalAlignment*(bounds().width()-m_leftMargin-m_rightMargin-modalWidth),
      m_topMargin+m_verticalAlignment*(bounds().height()-m_topMargin-m_bottomMargin-modalHeight), modalWidth, modalHeight);
    return modalViewFrame;
  }
  
  void ModalViewController::ContentView::layoutSubviews() {
    assert(m_regularView != nullptr);
    m_regularView->setFrame(bounds());
    if (m_isDisplayingModal) {
      assert(m_currentModalView != nullptr);
      m_currentModalView->setFrame(modalViewFrame());
    } else {
      if (m_currentModalView) {
        m_currentModalView->setFrame(KDRectZero);
      }
    }
  }
  
  void ModalViewController::ContentView::presentModalView(View * modalView, float verticalAlignment, float horizontalAlignment,
    KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin) {
    m_isDisplayingModal = true;
    m_currentModalView = modalView;
    m_horizontalAlignment = horizontalAlignment;
    m_verticalAlignment = verticalAlignment;
    m_topMargin = topMargin;
    m_leftMargin = leftMargin;
    m_bottomMargin = bottomMargin;
    m_rightMargin = rightMargin;
    layoutSubviews();
  }
  
  void ModalViewController::ContentView::dismissModalView() {
    m_isDisplayingModal = false;
    layoutSubviews();
    m_currentModalView->resetSuperview();
    m_currentModalView = nullptr;
  }
  
  bool ModalViewController::ContentView::isDisplayingModal() const {
    return m_isDisplayingModal;
  }
  
  void ModalViewController::ContentView::reload() {
    markRectAsDirty(bounds());
    layoutSubviews();
  }
  
  ModalViewController::ModalViewController(Responder * parentResponder, ViewController * child) :
    ViewController(parentResponder),
    m_contentView(),
    m_previousResponder(child),
    m_currentModalViewController(nullptr),
    m_regularViewController(child)
  {
  }
  
  View * ModalViewController::view() {
    return &m_contentView;
  }
  
  bool ModalViewController::isDisplayingModal() {
    return m_contentView.isDisplayingModal();
  }
  
  void ModalViewController::displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment,
    KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin) {
    m_currentModalViewController = vc;
    vc->setParentResponder(this);
    m_previousResponder = app()->firstResponder();
    m_contentView.presentModalView(vc->view(), verticalAlignment, horizontalAlignment, topMargin, leftMargin, bottomMargin, rightMargin);
    m_currentModalViewController->viewWillAppear();
    app()->setFirstResponder(vc);
  }
  
  void ModalViewController::reloadModalViewController() {
    m_contentView.layoutSubviews();
  }
  
  void ModalViewController::dismissModalViewController() {
    m_currentModalViewController->viewDidDisappear();
    app()->setFirstResponder(m_previousResponder);
    m_contentView.dismissModalView();
    m_currentModalViewController = nullptr;
  }
  
  void ModalViewController::didBecomeFirstResponder() {
    if (m_contentView.isDisplayingModal()) {
      app()->setFirstResponder(m_currentModalViewController);
    }
    app()->setFirstResponder(m_regularViewController);
  }
  
  bool ModalViewController::handleEvent(Ion::Events::Event event) {
    if (!m_contentView.isDisplayingModal()) {
      return false;
    }
    if (event == Ion::Events::Back) {
      dismissModalViewController();
      return true;
    }
    return false;
  }
  
  void ModalViewController::viewWillAppear() {
    m_contentView.setMainView(m_regularViewController->view());
    m_contentView.layoutSubviews();
    if (m_contentView.isDisplayingModal()) {
      m_currentModalViewController->viewWillAppear();
    }
    m_regularViewController->viewWillAppear();
  }
  
  void ModalViewController::viewDidDisappear() {
    if (m_contentView.isDisplayingModal()) {
      m_currentModalViewController->viewDidDisappear();
    }
    m_regularViewController->viewDidDisappear();
  }
  
  void ModalViewController::reloadView() {
    m_contentView.reload();
  }