mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 13:38:14 +08:00 
			
		
		
		
	Introduce Options and use them for Menu.
Introduce Options for components. This allows me to add new features, without updating functions signatures.
This commit is contained in:
		
				
					committed by
					
						
						Arthur Sonzogni
					
				
			
			
				
	
			
			
			
						parent
						
							82adc3b410
						
					
				
				
					commit
					cd84b187b3
				
			@@ -38,8 +38,10 @@ namespace ftxui {
 | 
			
		||||
///   entry 2
 | 
			
		||||
///   entry 3
 | 
			
		||||
/// ```
 | 
			
		||||
Component Menu(const std::vector<std::wstring>* entries, int* selected) {
 | 
			
		||||
  return Make<MenuBase>(entries, selected);
 | 
			
		||||
Component Menu(const std::vector<std::wstring>* entries,
 | 
			
		||||
               int* selected,
 | 
			
		||||
               ConstRef<MenuOption> option) {
 | 
			
		||||
  return Make<MenuBase>(entries, selected, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
@@ -47,8 +49,10 @@ MenuBase* MenuBase::From(Component component) {
 | 
			
		||||
  return static_cast<MenuBase*>(component.get());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MenuBase::MenuBase(const std::vector<std::wstring>* entries, int* selected)
 | 
			
		||||
    : entries_(entries), selected_(selected) {}
 | 
			
		||||
MenuBase::MenuBase(const std::vector<std::wstring>* entries,
 | 
			
		||||
                   int* selected,
 | 
			
		||||
                   ConstRef<MenuOption> option)
 | 
			
		||||
    : entries_(entries), selected_(selected), option_(option) {}
 | 
			
		||||
 | 
			
		||||
Element MenuBase::Render() {
 | 
			
		||||
  Elements elements;
 | 
			
		||||
@@ -58,9 +62,10 @@ Element MenuBase::Render() {
 | 
			
		||||
    bool is_focused = (focused == int(i)) && is_menu_focused;
 | 
			
		||||
    bool is_selected = (*selected_ == int(i));
 | 
			
		||||
 | 
			
		||||
    auto style = is_selected
 | 
			
		||||
                     ? (is_focused ? selected_focused_style : selected_style)
 | 
			
		||||
                     : (is_focused ? focused_style : normal_style);
 | 
			
		||||
    auto style = is_selected ? (is_focused ? option_->selected_focused_style
 | 
			
		||||
                                           : option_->selected_style)
 | 
			
		||||
                             : (is_focused ? option_->focused_style
 | 
			
		||||
                                           : option_->normal_style);
 | 
			
		||||
    auto focus_management = !is_selected      ? nothing
 | 
			
		||||
                            : is_menu_focused ? focus
 | 
			
		||||
                                              : select;
 | 
			
		||||
@@ -94,12 +99,12 @@ bool MenuBase::OnEvent(Event event) {
 | 
			
		||||
 | 
			
		||||
  if (*selected_ != old_selected) {
 | 
			
		||||
    focused = *selected_;
 | 
			
		||||
    on_change();
 | 
			
		||||
    option_->on_change();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::Return) {
 | 
			
		||||
    on_enter();
 | 
			
		||||
    option_->on_enter();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -119,7 +124,7 @@ bool MenuBase::OnMouseEvent(Event event) {
 | 
			
		||||
        event.mouse().motion == Mouse::Released) {
 | 
			
		||||
      if (*selected_ != i) {
 | 
			
		||||
        *selected_ = i;
 | 
			
		||||
        on_change();
 | 
			
		||||
        option_->on_change();
 | 
			
		||||
      }
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -26,30 +26,6 @@ std::wstring to_wstring(const std::string& s) {
 | 
			
		||||
#pragma warning(pop)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
StringRef::StringRef(std::wstring* ref) : borrowed_(ref) {}
 | 
			
		||||
StringRef::StringRef(std::wstring ref) : owned_(std::move(ref)) {}
 | 
			
		||||
StringRef::StringRef(const wchar_t* ref) : owned_(ref) {}
 | 
			
		||||
StringRef::StringRef(const char* ref) : owned_(to_wstring(std::string(ref))) {}
 | 
			
		||||
std::wstring& StringRef::operator*() {
 | 
			
		||||
  return borrowed_ ? *borrowed_ : owned_;
 | 
			
		||||
}
 | 
			
		||||
std::wstring* StringRef::operator->() {
 | 
			
		||||
  return borrowed_ ? borrowed_ : &owned_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ConstStringRef::ConstStringRef(const std::wstring* ref) : borrowed_(ref) {}
 | 
			
		||||
ConstStringRef::ConstStringRef(std::wstring ref) : owned_(std::move(ref)) {}
 | 
			
		||||
ConstStringRef::ConstStringRef(const wchar_t* ref) : owned_(ref) {}
 | 
			
		||||
ConstStringRef::ConstStringRef(const char* ref)
 | 
			
		||||
    : owned_(to_wstring(std::string(ref))) {}
 | 
			
		||||
 | 
			
		||||
const std::wstring& ConstStringRef::operator*() {
 | 
			
		||||
  return borrowed_ ? *borrowed_ : owned_;
 | 
			
		||||
}
 | 
			
		||||
const std::wstring* ConstStringRef::operator->() {
 | 
			
		||||
  return borrowed_ ? borrowed_ : &owned_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ftxui
 | 
			
		||||
 | 
			
		||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user