Skip to content

Commit

Permalink
mac: Simplify event handler in views
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 2, 2022
1 parent 69371e5 commit 4e65528
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 115 deletions.
2 changes: 2 additions & 0 deletions nativeui/button.cc
Expand Up @@ -13,6 +13,8 @@ namespace nu {
// static
const char Button::kClassName[] = "Button";

Button::~Button() = default;

void Button::SetImage(scoped_refptr<Image> image) {
image_ = std::move(image);
PlatformSetImage(image_.get());
Expand Down
3 changes: 1 addition & 2 deletions nativeui/button.h
Expand Up @@ -65,10 +65,9 @@ class NATIVEUI_EXPORT Button : public View {
// Events.
Signal<void(Button*)> on_click;

protected:
private:
~Button() override;

private:
void PlatformSetImage(Image* image);
void PlatformSetTitle(const std::string& title);

Expand Down
3 changes: 0 additions & 3 deletions nativeui/gtk/button_gtk.cc
Expand Up @@ -41,9 +41,6 @@ Button::Button(const std::string& title, Type type) {
g_signal_connect(GetNative(), "clicked", G_CALLBACK(OnButtonClick), this);
}

Button::~Button() {
}

void Button::MakeDefault() {
GtkWidget* button = GetNative();
g_object_set_data(G_OBJECT(button), "default", this);
Expand Down
3 changes: 0 additions & 3 deletions nativeui/gtk/picker_gtk.cc
Expand Up @@ -46,9 +46,6 @@ Picker::Picker(NativeView view) {
UpdateDefaultStyle();
}

Picker::~Picker() {
}

void Picker::AddItem(const std::string& text) {
// Check for duplicate item.
bool duplicate = false;
Expand Down
51 changes: 17 additions & 34 deletions nativeui/mac/button_mac.mm
Expand Up @@ -34,12 +34,28 @@

@interface NUButton : NSButton<NUView> {
@private
nu::Button* shell_;
nu::NUPrivate private_;
}
- (id)initWithShell:(nu::Button*)shell;
- (IBAction)onClick:(id)sender;
@end

@implementation NUButton

- (id)initWithShell:(nu::Button*)shell {
if ((self = [super init])) {
shell_ = shell;
[self setTarget:self];
[self setAction:@selector(onClick:)];
}
return self;
}

- (IBAction)onClick:(id)sender {
shell_->on_click.Emit(shell_);
}

- (nu::NUPrivate*)nuPrivate {
return &private_;
}
Expand Down Expand Up @@ -89,53 +105,20 @@ - (NSRect)frame {

@end

// The button delegate to catch click events.
@interface NUButtonDelegate : NSObject {
@private
nu::Button* shell_;
}
- (id)initWithShell:(nu::Button*)shell;
- (IBAction)onClick:(id)sender;
@end

@implementation NUButtonDelegate

- (id)initWithShell:(nu::Button*)shell {
if ((self = [super init]))
shell_ = shell;
return self;
}

- (IBAction)onClick:(id)sender {
shell_->on_click.Emit(shell_);
}

@end

namespace nu {

Button::Button(const std::string& title, Type type) {
NSButton* button = [[NUButton alloc] init];
NSButton* button = [[NUButton alloc] initWithShell:this];
if (type == Type::Normal)
[button setBezelStyle:NSRoundedBezelStyle];
else if (type == Type::Checkbox)
[button setButtonType:NSSwitchButton];
else if (type == Type::Radio)
[button setButtonType:NSRadioButton];

[button setTarget:[[NUButtonDelegate alloc] initWithShell:this]];
[button setAction:@selector(onClick:)];
TakeOverView(button);

SetTitle(title);
}

Button::~Button() {
NSButton* button = static_cast<NSButton*>(GetNative());
[button.target release];
[button setTarget:nil];
}

void Button::MakeDefault() {
NSButton* button = static_cast<NSButton*>(GetNative());
[button setKeyEquivalent:@"\r"];
Expand Down
57 changes: 19 additions & 38 deletions nativeui/mac/picker_mac.mm
Expand Up @@ -10,12 +10,30 @@

@interface NUPicker : NSPopUpButton<NUView> {
@private
nu::Picker* shell_;
nu::NUPrivate private_;
}
- (id)initWithShell:(nu::Picker*)shell;
- (IBAction)onSelectionChange:(id)sender;
@end

@implementation NUPicker

- (id)initWithShell:(nu::Picker*)shell {
if ((self = [super initWithFrame:NSZeroRect pullsDown:NO])) {
shell_ = shell;
[self setTarget:self];
[self setAction:@selector(onSelectionChange:)];
[self setPreferredEdge:NSMinYEdge];
[[self cell] setArrowPosition:NSPopUpArrowAtBottom];
}
return self;
}

- (IBAction)onSelectionChange:(id)sender {
shell_->on_selection_change.Emit(shell_);
}

- (nu::NUPrivate*)nuPrivate {
return &private_;
}
Expand All @@ -40,38 +58,9 @@ - (BOOL)isNUEnabled {

@end

@interface NUPickerDelegate : NSObject {
@private
nu::Picker* shell_;
}
- (id)initWithShell:(nu::Picker*)shell;
- (IBAction)onChange:(id)sender;
@end

@implementation NUPickerDelegate

- (id)initWithShell:(nu::Picker*)shell {
if ((self = [super init]))
shell_ = shell;
return self;
}

- (IBAction)onChange:(id)sender {
shell_->on_selection_change.Emit(shell_);
}

@end

namespace nu {

Picker::Picker()
: Picker([[NUPicker alloc] initWithFrame:NSZeroRect pullsDown:NO]) {
auto* picker = static_cast<NUPicker*>(GetNative());
[picker setTarget:[[NUPickerDelegate alloc] initWithShell:this]];
[picker setAction:@selector(onChange:)];
[picker setPreferredEdge:NSMinYEdge];
[[picker cell] setArrowPosition:NSPopUpArrowAtBottom];

Picker::Picker() : Picker([[NUPicker alloc] initWithShell:this]) {
// The subclass overrides GetMinimumSize, so we must let subclass call
// UpdateDefaultStyle itself.
UpdateDefaultStyle();
Expand All @@ -81,14 +70,6 @@ - (IBAction)onChange:(id)sender {
TakeOverView(view);
}

Picker::~Picker() {
auto* picker = static_cast<NSControl*>(GetNative());
if (picker.target) {
[picker.target release];
[picker setTarget:nil];
}
}

void Picker::AddItem(const std::string& text) {
auto* picker = static_cast<NUPicker*>(GetNative());
[picker addItemWithTitle:base::SysUTF8ToNSString(text)];
Expand Down
35 changes: 8 additions & 27 deletions nativeui/mac/slider_mac.mm
Expand Up @@ -4,23 +4,27 @@

#include "nativeui/slider.h"

#include "base/mac/scoped_nsobject.h"
#include "nativeui/mac/nu_private.h"
#include "nativeui/mac/nu_view.h"

@interface NUSliderDelegate : NSObject {
@interface NUSlider : NSSlider<NUView> {
@private
nu::Slider* shell_;
nu::NUPrivate private_;
}
- (id)initWithShell:(nu::Slider*)shell;
- (IBAction)onValueChange:(id)sender;
@end

@implementation NUSliderDelegate
@implementation NUSlider

- (id)initWithShell:(nu::Slider*)shell {
if ((self = [super init]))
if ((self = [super init])) {
shell_ = shell;
[self setTarget:self];
[self setAction:@selector(onValueChange:)];
[self setMaxValue:100.];
}
return self;
}

Expand All @@ -32,29 +36,6 @@ - (IBAction)onValueChange:(id)sender {
shell_->on_sliding_complete.Emit(shell_);
}

@end


@interface NUSlider : NSSlider<NUView> {
@private
base::scoped_nsobject<NUSliderDelegate> delegate_;
nu::NUPrivate private_;
}
- (id)initWithShell:(nu::Slider*)shell;
@end

@implementation NUSlider

- (id)initWithShell:(nu::Slider*)shell {
if ((self = [super init])) {
delegate_.reset([[NUSliderDelegate alloc] initWithShell:shell]);
[self setMaxValue:100.];
[self setTarget:delegate_];
[self setAction:@selector(onValueChange:)];
}
return self;
}

- (nu::NUPrivate*)nuPrivate {
return &private_;
}
Expand Down
2 changes: 2 additions & 0 deletions nativeui/picker.cc
Expand Up @@ -9,6 +9,8 @@ namespace nu {
// static
const char Picker::kClassName[] = "Picker";

Picker::~Picker() = default;

const char* Picker::GetClassName() const {
return kClassName;
}
Expand Down
4 changes: 2 additions & 2 deletions nativeui/picker.h
Expand Up @@ -36,10 +36,10 @@ class NATIVEUI_EXPORT Picker : public View {
Signal<void(Picker*)> on_selection_change;

protected:
~Picker() override;

// Used by subclass.
explicit Picker(NativeView view);

~Picker() override;
};

} // namespace nu
Expand Down
3 changes: 0 additions & 3 deletions nativeui/win/button_win.cc
Expand Up @@ -241,9 +241,6 @@ Button::Button(const std::string& title, Type type) {
SetTitle(title);
}

Button::~Button() {
}

void Button::MakeDefault() {
auto* button = static_cast<ButtonImpl*>(GetNative());
button->MakeDefault();
Expand Down
3 changes: 0 additions & 3 deletions nativeui/win/picker_win.cc
Expand Up @@ -58,9 +58,6 @@ Picker::Picker(NativeView view) {
UpdateDefaultStyle();
}

Picker::~Picker() {
}

void Picker::AddItem(const std::string& text) {
auto* picker = static_cast<PickerImpl*>(GetNative());
std::wstring text16 = base::UTF8ToWide(text);
Expand Down

0 comments on commit 4e65528

Please sign in to comment.