2021-05-02 02:40:35 +08:00
|
|
|
#include <functional> // for function
|
|
|
|
#include <memory> // for shared_ptr
|
2020-08-26 22:26:09 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/component/button.hpp"
|
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::Return
|
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
2020-08-26 22:26:09 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
Element Button::Render() {
|
2021-04-25 00:16:13 +08:00
|
|
|
auto style = Focused() ? inverted : nothing;
|
|
|
|
return text(label) | border | style | reflect(box_);
|
2020-08-26 22:26:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::OnEvent(Event event) {
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
|
2021-05-02 02:40:35 +08:00
|
|
|
if (!CaptureMouse(event))
|
2021-05-02 00:13:56 +08:00
|
|
|
return false;
|
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
TakeFocus();
|
|
|
|
|
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
2021-04-19 04:33:41 +08:00
|
|
|
on_click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-26 22:26:09 +08:00
|
|
|
if (event == Event::Return) {
|
|
|
|
on_click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ftxui
|
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|