mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 13:38:14 +08:00 
			
		
		
		
	Remove button.hpp
This commit is contained in:
		
				
					committed by
					
						
						Arthur Sonzogni
					
				
			
			
				
	
			
			
			
						parent
						
							f53dc139e9
						
					
				
				
					commit
					e66ebe5443
				
			@@ -1,14 +1,65 @@
 | 
			
		||||
#include <functional>  // for function
 | 
			
		||||
#include <memory>      // for shared_ptr
 | 
			
		||||
 | 
			
		||||
#include "ftxui/component/button.hpp"
 | 
			
		||||
#include "ftxui/component/captured_mouse.hpp"  // for CapturedMouse
 | 
			
		||||
#include "ftxui/component/component.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
 | 
			
		||||
 | 
			
		||||
namespace ftxui {
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
/// @brief A button. An action is associated to the click event.
 | 
			
		||||
/// @ingroup dom
 | 
			
		||||
class ButtonBase : public ComponentBase {
 | 
			
		||||
 public:
 | 
			
		||||
  ButtonBase(ConstStringRef label,
 | 
			
		||||
             std::function<void()> on_click,
 | 
			
		||||
             ConstRef<ButtonOption> option)
 | 
			
		||||
      : label_(label), on_click_(on_click), option_(std::move(option)) {}
 | 
			
		||||
 | 
			
		||||
  ~ButtonBase() override = default;
 | 
			
		||||
 | 
			
		||||
  // Component implementation:
 | 
			
		||||
  Element Render() override {
 | 
			
		||||
    auto style = Focused() ? inverted : nothing;
 | 
			
		||||
    auto my_border = option_->border ? border : nothing;
 | 
			
		||||
    return text(*label_) | my_border | style | reflect(box_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool OnEvent(Event event) override {
 | 
			
		||||
    if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
 | 
			
		||||
      if (!CaptureMouse(event))
 | 
			
		||||
        return false;
 | 
			
		||||
 | 
			
		||||
      TakeFocus();
 | 
			
		||||
 | 
			
		||||
      if (event.mouse().button == Mouse::Left &&
 | 
			
		||||
          event.mouse().motion == Mouse::Pressed) {
 | 
			
		||||
        on_click_();
 | 
			
		||||
        return true;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::Return) {
 | 
			
		||||
      on_click_();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  ConstStringRef label_;
 | 
			
		||||
  std::function<void()> on_click_;
 | 
			
		||||
  Box box_;
 | 
			
		||||
  ConstRef<ButtonOption> option_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace
 | 
			
		||||
 | 
			
		||||
/// @brief Draw a button. Execute a function when clicked.
 | 
			
		||||
/// @param label The label of the button.
 | 
			
		||||
/// @param on_click The action to execute when clicked.
 | 
			
		||||
@@ -37,45 +88,6 @@ Component Button(ConstStringRef label,
 | 
			
		||||
  return Make<ButtonBase>(label, std::move(on_click), std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
ButtonBase* ButtonBase::From(Component component) {
 | 
			
		||||
  return static_cast<ButtonBase*>(component.get());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ButtonBase::ButtonBase(ConstStringRef label,
 | 
			
		||||
                       std::function<void()> on_click,
 | 
			
		||||
                       ConstRef<ButtonOption> option)
 | 
			
		||||
    : label_(label), on_click_(on_click), option_(std::move(option)) {}
 | 
			
		||||
 | 
			
		||||
Element ButtonBase::Render() {
 | 
			
		||||
  auto style = Focused() ? inverted : nothing;
 | 
			
		||||
  auto my_border = option_->border ? border : nothing;
 | 
			
		||||
  return text(*label_) | my_border | style | reflect(box_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ButtonBase::OnEvent(Event event) {
 | 
			
		||||
  if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
 | 
			
		||||
    if (!CaptureMouse(event))
 | 
			
		||||
      return false;
 | 
			
		||||
 | 
			
		||||
    TakeFocus();
 | 
			
		||||
 | 
			
		||||
    if (event.mouse().button == Mouse::Left &&
 | 
			
		||||
        event.mouse().motion == Mouse::Pressed) {
 | 
			
		||||
      on_click_();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::Return) {
 | 
			
		||||
    on_click_();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ftxui
 | 
			
		||||
 | 
			
		||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user