Skip to content

Commit

Permalink
cocoa: Import from former external "gocoa" module. (#13)
Browse files Browse the repository at this point in the history
Contrary to my former belief (as expressed in the README of my gocoa fork[1] and mojbro/gocoa#24), going forward I think that maintaining gocoa as a separate Go module is too much of a burden as I'm holding back lots of small things which would need Spot-specific code right now. Honestly, most of my existing changes should have been broader to make sense for a general purpose Cocoa binding.

To make things a bit easier for me, I decided to import the binding completely into Spot as an internal package.

I'll be removing anything that I don't seem necessary for Spot but I will keep the original history to correctly attribute the work done by @mojbro, @phaus, @StarHack, and @dim13

[1] https://github.com/roblillack/gocoa?tab=readme-ov-file#what-is-this
  • Loading branch information
roblillack authored May 29, 2024
2 parents d24e6b8 + de1b8c7 commit b608150
Show file tree
Hide file tree
Showing 68 changed files with 3,018 additions and 58 deletions.
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ module github.com/roblillack/spot

go 1.21

require (
github.com/pwiecz/go-fltk v0.0.0-20240511142305-990b442ae1ed
github.com/roblillack/gocoa v0.3.0
)

// replace github.com/roblillack/gocoa => ./libs/gocoa
require github.com/pwiecz/go-fltk v0.0.0-20240511142305-990b442ae1ed
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/pwiecz/go-fltk v0.0.0-20240511142305-990b442ae1ed h1:7/j/4x3KAwuJB9sWdfhi+1UFbv42TyJ4LyUI/GHS+p8=
github.com/pwiecz/go-fltk v0.0.0-20240511142305-990b442ae1ed/go.mod h1:uMK5daOr9p+ba2BPs5QadbfaqqrHR5TGj13yWGsAsmw=
github.com/roblillack/gocoa v0.3.0 h1:ZvEulTbrJHgmeweJJrHZhoGvUzgD/Q54Pmc+cJ5sJyo=
github.com/roblillack/gocoa v0.3.0/go.mod h1:zySUFzLK/KSb3KDT903dTRJ2WPmMBbF3a2u8/39UzMg=
6 changes: 3 additions & 3 deletions ui/button_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package ui

import (
"github.com/roblillack/gocoa"
"github.com/roblillack/spot"
"github.com/roblillack/spot/ui/internal/cocoa"
)

type nativeTypeButton = *gocoa.Button
type nativeTypeButton = *cocoa.Button

func (b *Button) Update(nextComponent spot.Control) bool {
next, ok := nextComponent.(*Button)
Expand All @@ -34,7 +34,7 @@ func (b *Button) Mount(parent spot.Control) any {
return b.ref
}

b.ref = gocoa.NewButton(b.X, b.Y, b.Width, b.Height)
b.ref = cocoa.NewButton(b.X, b.Y, b.Width, b.Height)
b.ref.SetTitle(b.Title)
b.ref.OnClick(b.OnClick)

Expand Down
20 changes: 10 additions & 10 deletions ui/checkbox_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
package ui

import (
"github.com/roblillack/gocoa"
"github.com/roblillack/spot"
"github.com/roblillack/spot/ui/internal/cocoa"
)

type nativeTypeCheckbox = *gocoa.Button
type nativeTypeCheckbox = *cocoa.Button

func (w *Checkbox) onClick() {
w.Checked = !w.Checked
if w.Checked {
w.ref.SetState(gocoa.ButtonStateValueOn)
w.ref.SetState(cocoa.ButtonStateValueOn)
} else {
w.ref.SetState(gocoa.ButtonStateValueOff)
w.ref.SetState(cocoa.ButtonStateValueOff)
}
w.OnChange(w.Checked)
}
Expand All @@ -32,9 +32,9 @@ func (w *Checkbox) Update(nextComponent spot.Control) bool {
if next.Checked != w.Checked {
w.Checked = next.Checked
if w.Checked {
w.ref.SetState(gocoa.ButtonStateValueOn)
w.ref.SetState(cocoa.ButtonStateValueOn)
} else {
w.ref.SetState(gocoa.ButtonStateValueOff)
w.ref.SetState(cocoa.ButtonStateValueOff)
}
}

Expand All @@ -58,13 +58,13 @@ func (w *Checkbox) Mount(parent spot.Control) any {
return w.ref
}

w.ref = gocoa.NewButton(w.X, w.Y, w.Width, w.Height)
w.ref = cocoa.NewButton(w.X, w.Y, w.Width, w.Height)
w.ref.SetTitle(w.Label)
w.ref.SetButtonType(gocoa.ButtonTypeSwitch)
w.ref.SetButtonType(cocoa.ButtonTypeSwitch)
if w.Checked {
w.ref.SetState(gocoa.ButtonStateValueOn)
w.ref.SetState(cocoa.ButtonStateValueOn)
} else {
w.ref.SetState(gocoa.ButtonStateValueOff)
w.ref.SetState(cocoa.ButtonStateValueOff)
}
if w.OnChange == nil {
w.ref.OnClick(nil)
Expand Down
8 changes: 4 additions & 4 deletions ui/dial_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package ui

import (
"github.com/roblillack/gocoa"
"github.com/roblillack/spot"
"github.com/roblillack/spot/ui/internal/cocoa"
)

type nativeTypeDial = *gocoa.Slider
type nativeTypeDial = *cocoa.Slider

func (b *Dial) Update(nextComponent spot.Control) bool {
next, ok := nextComponent.(*Dial)
Expand Down Expand Up @@ -42,11 +42,11 @@ func (b *Dial) Mount(parent spot.Control) any {
return b.ref
}

b.ref = gocoa.NewSlider(b.X, b.Y, b.Width, b.Height)
b.ref = cocoa.NewSlider(b.X, b.Y, b.Width, b.Height)
b.ref.SetMaximumValue(b.Max)
b.ref.SetMinimumValue(b.Min)
b.ref.SetValue(b.Value)
b.ref.SetSliderType(gocoa.SliderTypeCircular)
b.ref.SetSliderType(cocoa.SliderTypeCircular)
b.ref.OnSliderValueChanged(func() {
if b.OnValueChanged != nil {
b.OnValueChanged(b.ref.Value())
Expand Down
6 changes: 3 additions & 3 deletions ui/dropdown_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
package ui

import (
"github.com/roblillack/gocoa"
"github.com/roblillack/spot"
"github.com/roblillack/spot/ui/internal/cocoa"
)

type nativeTypeDropdown = *gocoa.ComboBox
type nativeTypeDropdown = *cocoa.ComboBox

func (c *Dropdown) Mount(parent spot.Control) any {
if c.ref != nil {
return c.ref
}

c.ref = gocoa.NewComboBox(c.X, c.Y, c.Width, c.Height)
c.ref = cocoa.NewComboBox(c.X, c.Y, c.Width, c.Height)
for _, item := range c.Items {
c.ref.AddItem(item)
}
Expand Down
8 changes: 4 additions & 4 deletions ui/init_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package ui
import (
"runtime"

"github.com/roblillack/gocoa"
"github.com/roblillack/spot"
"github.com/roblillack/spot/ui/internal/cocoa"
)

// BackendName is the name of the backend. It can be used to check which backend
Expand All @@ -16,13 +16,13 @@ const BackendName = "cocoa"
// Init initializes the UI library for the Cocoa backend. It locks the OS thread
// and sets up Spot to be able to intercept the main loop.
func Init() {
spot.RunOnMainLoop = gocoa.RunOnMainLoop
spot.RunOnMainLoop = cocoa.RunOnMainLoop

runtime.LockOSThread()
gocoa.InitApplication()
cocoa.InitApplication()
}

// Run starts the main loop for the Cocoa backend.
func Run() {
gocoa.RunApplication()
cocoa.RunApplication()
}
21 changes: 21 additions & 0 deletions ui/internal/cocoa/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Philip Möjbro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions ui/internal/cocoa/appdelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@end
21 changes: 21 additions & 0 deletions ui/internal/cocoa/appdelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#import "appdelegate.h"
#include "_cgo_export.h"

@implementation AppDelegate

- (void)dealloc
{
[super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"applicationDidFinishLaunching");

[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];

callOnApplicationDidFinishLaunchingHandler();
}

@end
50 changes: 50 additions & 0 deletions ui/internal/cocoa/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cocoa

// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #include "application.h"
import "C"
import "runtime/cgo"

var appDidFinishLaunchingFunc func()

// InitApplication initializes the global application instance. Call this before using
// the rest of the cocoa package.
func InitApplication() {
C.InitSharedApplication()
}

// RunApplication launches the main Cocoa runloop.
func RunApplication() {
C.RunApplication()
}

// OnApplicationDidFinishLaunching - will be triggered after Application Launch is finished
func OnApplicationDidFinishLaunching(fn func()) {
appDidFinishLaunchingFunc = fn
}

// TerminateApplication - will be triggered, when the Application terminates
func TerminateApplication() {
C.TerminateApplication()
}

//export callOnApplicationDidFinishLaunchingHandler
func callOnApplicationDidFinishLaunchingHandler() {
if appDidFinishLaunchingFunc != nil {
appDidFinishLaunchingFunc()
}
}

//export go_callback
func go_callback(h C.uintptr_t) {
hnd := cgo.Handle(h)
fn := hnd.Value().(func())
fn()
hnd.Delete()
}

func RunOnMainLoop(fn func()) {
h := cgo.NewHandle(fn)
C.RunOnMainLoop(C.uintptr_t(h))
}
7 changes: 7 additions & 0 deletions ui/internal/cocoa/application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdint.h>
#import <Cocoa/Cocoa.h>

void InitSharedApplication();
void RunApplication();
void TerminateApplication();
void RunOnMainLoop(uintptr_t h);
41 changes: 41 additions & 0 deletions ui/internal/cocoa/application.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#import "application.h"
#import "appdelegate.h"
#include "_cgo_export.h"

AppDelegate *gocoa_applicationDelegate = nil;

// InitSharedApplication calls NSApplication.sharedApplication() which initializes the
// global application instance NSApp.
void InitSharedApplication() {
static bool hasBeenInitialized = false; // false first time function is called
if (hasBeenInitialized)
return;
[NSApplication sharedApplication];
gocoa_applicationDelegate = [[AppDelegate alloc] init];
[NSApp setDelegate:gocoa_applicationDelegate];
hasBeenInitialized = true;
}

void releaseSharedApplication() {
if (gocoa_applicationDelegate != nil) {
[gocoa_applicationDelegate release];
}
}

void RunApplication() {
@autoreleasepool {
[NSApp run];
releaseSharedApplication();
}
}

void TerminateApplication() {
[NSApp terminate:nil];
}

extern void go_callback(uintptr_t h);
void RunOnMainLoop(uintptr_t h) {
dispatch_async(dispatch_get_main_queue(), ^{
go_callback(h);
});
}
Loading

0 comments on commit b608150

Please sign in to comment.