2020-05-26 06:51:06 +00:00
|
|
|
package gts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
2020-06-07 04:27:28 +00:00
|
|
|
"github.com/gotk3/gotk3/gdk"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/gotk3/gotk3/glib"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
const AppID = "com.github.diamondburned.cchat-gtk"
|
|
|
|
|
2020-05-26 06:51:06 +00:00
|
|
|
var Args = append([]string{}, os.Args...)
|
|
|
|
|
|
|
|
var App struct {
|
|
|
|
*gtk.Application
|
|
|
|
Window *gtk.ApplicationWindow
|
|
|
|
Header *gtk.HeaderBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
gtk.Init(&Args)
|
2020-06-06 00:47:28 +00:00
|
|
|
App.Application, _ = gtk.ApplicationNew(AppID, 0)
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Windower interface {
|
|
|
|
Window() gtk.IWidget
|
|
|
|
}
|
|
|
|
|
|
|
|
type Headerer interface {
|
|
|
|
Header() gtk.IWidget
|
|
|
|
}
|
|
|
|
|
|
|
|
// Above interfaces should be kept for modularity, but since this is an internal
|
|
|
|
// abstraction, we already know our application will implement both.
|
|
|
|
type WindowHeaderer interface {
|
|
|
|
Windower
|
|
|
|
Headerer
|
|
|
|
}
|
|
|
|
|
|
|
|
func Main(wfn func() WindowHeaderer) {
|
|
|
|
App.Application.Connect("activate", func() {
|
2020-06-06 00:47:28 +00:00
|
|
|
// Load all CSS onto the default screen.
|
|
|
|
loadProviders(getDefaultScreen())
|
|
|
|
|
2020-05-26 06:51:06 +00:00
|
|
|
App.Header, _ = gtk.HeaderBarNew()
|
|
|
|
App.Header.SetShowCloseButton(true)
|
2020-05-28 19:26:55 +00:00
|
|
|
App.Header.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
b, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
|
|
|
|
App.Header.SetCustomTitle(b)
|
|
|
|
|
2020-05-26 06:51:06 +00:00
|
|
|
App.Window, _ = gtk.ApplicationWindowNew(App.Application)
|
2020-06-04 23:00:41 +00:00
|
|
|
App.Window.SetDefaultSize(1000, 500)
|
2020-05-26 06:51:06 +00:00
|
|
|
App.Window.SetTitlebar(App.Header)
|
2020-05-28 19:26:55 +00:00
|
|
|
App.Window.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
// Execute the function later, because we need it to run after
|
|
|
|
// initialization.
|
|
|
|
w := wfn()
|
|
|
|
App.Window.Add(w.Window())
|
|
|
|
App.Header.Add(w.Header())
|
|
|
|
})
|
|
|
|
|
|
|
|
// Use a special function to run the application. Exit with the appropriate
|
|
|
|
// exit code.
|
|
|
|
os.Exit(App.Run(Args))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async runs fn asynchronously, then runs the function it returns in the Gtk
|
|
|
|
// main thread.
|
|
|
|
func Async(fn func() (func(), error)) {
|
|
|
|
go func() {
|
|
|
|
f, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Attempt to run the callback if it's there.
|
|
|
|
if f != nil {
|
|
|
|
glib.IdleAdd(f)
|
|
|
|
}
|
2020-05-26 06:51:06 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecAsync executes function asynchronously in the Gtk main thread.
|
|
|
|
func ExecAsync(fn func()) {
|
|
|
|
glib.IdleAdd(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecSync executes the function asynchronously, but returns a channel that
|
|
|
|
// indicates when the job is done.
|
|
|
|
func ExecSync(fn func()) <-chan struct{} {
|
2020-06-07 04:27:28 +00:00
|
|
|
var ch = make(chan struct{})
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
glib.IdleAdd(func() {
|
|
|
|
fn()
|
|
|
|
close(ch)
|
|
|
|
})
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
2020-06-07 04:27:28 +00:00
|
|
|
|
|
|
|
func EventIsRightClick(ev *gdk.Event) bool {
|
|
|
|
keyev := gdk.EventButtonNewFromEvent(ev)
|
|
|
|
return keyev.Type() == gdk.EVENT_BUTTON_PRESS && keyev.Button() == gdk.BUTTON_SECONDARY
|
|
|
|
}
|