Skip to content

Commit 7dc6ff3

Browse files
committed
fix(graphics): refactored graphics api
Signed-off-by: Manjeet Singh <itsmanjeet1998@gmail.com>
1 parent ba0b20d commit 7dc6ff3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+3710
-3790
lines changed

api/display/shm.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package display
22

33
import (
44
"fmt"
5+
"image"
56
"log"
67
"os"
78
"sync"
89
"syscall"
910
"time"
1011

1112
"avyos.dev/pkg/fs"
12-
graphics "avyos.dev/pkg/graphics/input"
13+
gfxinput "avyos.dev/pkg/graphics/input"
14+
core "avyos.dev/pkg/graphics/pixmap"
1315
)
1416

1517
const (
@@ -82,9 +84,9 @@ type Event struct {
8284
Scope uint32
8385
X, Y int
8486
Button int
85-
Key graphics.Key
87+
Key gfxinput.Key
8688
Rune rune
87-
Modifiers graphics.Modifiers
89+
Modifiers gfxinput.Modifiers
8890
Char rune
8991
Pressed bool
9092
Focused bool
@@ -111,7 +113,7 @@ type ClientWindow struct {
111113
path string
112114
data []byte
113115
stride int
114-
buf *graphics.Buffer
116+
buf *core.Buffer
115117
retired []sharedBuffer
116118
}
117119

@@ -188,7 +190,7 @@ func (cl *DisplayClient) hookEvents() {
188190
Scope: ev.Scope,
189191
Key: ev.Key,
190192
Rune: ev.Rune,
191-
Modifiers: graphics.Modifiers(ev.Modifiers),
193+
Modifiers: gfxinput.Modifiers(ev.Modifiers),
192194
})
193195
})
194196
}
@@ -273,11 +275,11 @@ func (cl *DisplayClient) registerWindow(id uint32, x, y, width, height int, shm
273275
path: shm.path,
274276
data: shm.data,
275277
stride: shm.stride,
276-
buf: &graphics.Buffer{
278+
buf: &core.Buffer{
277279
Width: width,
278280
Height: height,
279281
Stride: shm.stride,
280-
Format: graphics.PixelFormatBGRA,
282+
Format: core.PixelFormatBGRA,
281283
Data: shm.data,
282284
},
283285
}
@@ -391,18 +393,18 @@ func (cl *DisplayClient) SetWindowState(windowID, action uint32) error {
391393
return err
392394
}
393395

394-
func (cl *DisplayClient) RegisterShortcut(shortcutID, windowID, scope uint32, key graphics.Key, modifiers graphics.Modifiers) error {
396+
func (cl *DisplayClient) RegisterShortcut(shortcutID, windowID, scope uint32, key gfxinput.Key, modifiers gfxinput.Modifiers) error {
395397
return cl.RegisterShortcutEx(shortcutID, windowID, scope, key, 0, modifiers)
396398
}
397399

398-
func (cl *DisplayClient) RegisterShortcutEx(shortcutID, windowID, scope uint32, key graphics.Key, ch rune, modifiers graphics.Modifiers) error {
400+
func (cl *DisplayClient) RegisterShortcutEx(shortcutID, windowID, scope uint32, key gfxinput.Key, ch rune, modifiers gfxinput.Modifiers) error {
399401
_, err := cl.rpc.RegisterShortcut(RegisterShortcutRequest{
400402
ShortcutID: shortcutID,
401403
WindowID: windowID,
402404
Scope: scope,
403405
Key: key,
404406
Rune: ch,
405-
Modifiers: uint8(modifiers & (graphics.ModShift | graphics.ModCtrl | graphics.ModAlt)),
407+
Modifiers: uint8(modifiers & (gfxinput.ModShift | gfxinput.ModCtrl | gfxinput.ModAlt)),
406408
})
407409
return err
408410
}
@@ -448,17 +450,17 @@ func (cl *DisplayClient) Close() error {
448450
return cl.rpc.Close()
449451
}
450452

451-
func (w *ClientWindow) Buffer() *graphics.Buffer {
453+
func (w *ClientWindow) Buffer() *core.Buffer {
452454
return w.buf
453455
}
454456

455-
func (w *ClientWindow) Damage(r graphics.Rect) error {
456-
_, err := w.client.rpc.Damage(DamageRequest{WindowID: w.ID, X: r.X, Y: r.Y, Width: r.W, Height: r.H})
457+
func (w *ClientWindow) Damage(r image.Rectangle) error {
458+
_, err := w.client.rpc.Damage(DamageRequest{WindowID: w.ID, X: r.Min.X, Y: r.Min.Y, Width: r.Dx(), Height: r.Dy()})
457459
return err
458460
}
459461

460462
func (w *ClientWindow) DamageAll() error {
461-
return w.Damage(graphics.Rect{W: w.Width, H: w.Height})
463+
return w.Damage(core.RectXYWH(0, 0, w.Width, w.Height))
462464
}
463465

464466
func (w *ClientWindow) SetTitle(title string) error {
@@ -506,11 +508,11 @@ func (w *ClientWindow) Resize(width, height int) error {
506508
w.stride = shm.stride
507509
w.Width = width
508510
w.Height = height
509-
w.buf = &graphics.Buffer{
511+
w.buf = &core.Buffer{
510512
Width: width,
511513
Height: height,
512514
Stride: shm.stride,
513-
Format: graphics.PixelFormatBGRA,
515+
Format: core.PixelFormatBGRA,
514516
Data: shm.data,
515517
}
516518
return nil

apps/background/main.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"flag"
66
"fmt"
7+
"image/color"
78
"os"
89
"os/exec"
910
"strings"
@@ -12,10 +13,10 @@ import (
1213
display "avyos.dev/api/display"
1314
settingsapi "avyos.dev/api/settings"
1415
gapp "avyos.dev/pkg/graphics/app"
15-
declapp "avyos.dev/pkg/graphics/app/decl"
1616
displaybackend "avyos.dev/pkg/graphics/backend/display"
17-
graphics "avyos.dev/pkg/graphics/input"
18-
gfxtheme "avyos.dev/pkg/graphics/theme"
17+
gfxinput "avyos.dev/pkg/graphics/input"
18+
core "avyos.dev/pkg/graphics/pixmap"
19+
gfxtheme "avyos.dev/pkg/graphics/themes"
1920
"avyos.dev/pkg/logger"
2021
)
2122

@@ -43,7 +44,7 @@ type options struct {
4344
mode string
4445
imagePath string
4546
scaleMode string
46-
color graphics.Color
47+
color color.NRGBA
4748
}
4849

4950
var (
@@ -74,7 +75,7 @@ func init() {
7475
}
7576

7677
type BackgroundApp struct {
77-
declapp.App
78+
gapp.App
7879
mouseX int
7980
mouseY int
8081
}
@@ -112,17 +113,17 @@ func run() error {
112113
app.Configure(func(core *gapp.App) {
113114
core.OnEvent = app.handleCoreEvent
114115
})
115-
if err := app.RegisterClientShortcut(shortcutDesktopMenu, 0, graphics.KeyF10, 0, graphics.ModShift, func(graphics.Event) {
116+
if err := app.RegisterClientShortcut(shortcutDesktopMenu, 0, gfxinput.KeyF10, 0, gfxinput.ModShift, func(gfxinput.Event) {
116117
app.openDesktopMenuAtPointer()
117118
}); err != nil {
118119
return err
119120
}
120-
if err := app.RegisterGlobalShortcut(shortcutLaunchpad, graphics.KeySpace, 0, graphics.ModCtrl, func(graphics.Event) {
121+
if err := app.RegisterGlobalShortcut(shortcutLaunchpad, gfxinput.KeySpace, 0, gfxinput.ModCtrl, func(gfxinput.Event) {
121122
app.OpenLaunchpad()
122123
}); err != nil {
123124
return err
124125
}
125-
if err := app.RegisterGlobalShortcut(shortcutTerminal, graphics.KeyNone, 't', graphics.ModCtrl|graphics.ModAlt, func(graphics.Event) {
126+
if err := app.RegisterGlobalShortcut(shortcutTerminal, gfxinput.KeyNone, 't', gfxinput.ModCtrl|gfxinput.ModAlt, func(gfxinput.Event) {
126127
app.OpenTerminal()
127128
}); err != nil {
128129
return err
@@ -144,20 +145,20 @@ func run() error {
144145
return nil
145146
}
146147

147-
func (a *BackgroundApp) handleCoreEvent(ev graphics.Event) bool {
148+
func (a *BackgroundApp) handleCoreEvent(ev gfxinput.Event) bool {
148149
switch ev.Type {
149-
case graphics.EventMouseMove:
150+
case gfxinput.EventMouseMove:
150151
a.mouseX = ev.X
151152
a.mouseY = ev.Y
152-
case graphics.EventMouseButtonRelease:
153-
if ev.MouseButton == graphics.MouseButtonRight {
153+
case gfxinput.EventMouseButtonRelease:
154+
if ev.MouseButton == gfxinput.MouseButtonRight {
154155
a.mouseX = ev.X
155156
a.mouseY = ev.Y
156157
return a.OpenMenu("DesktopMenu", ev.X, ev.Y)
157158
}
158-
case graphics.EventMouseButtonPress:
159+
case gfxinput.EventMouseButtonPress:
159160
// Dismiss open desktop menu quickly when user clicks the wallpaper.
160-
if ev.MouseButton == graphics.MouseButtonLeft {
161+
if ev.MouseButton == gfxinput.MouseButtonLeft {
161162
a.CloseMenu()
162163
}
163164
}
@@ -169,8 +170,8 @@ func (a *BackgroundApp) openDesktopMenuAtPointer() {
169170
if x < 0 || y < 0 {
170171
if root := a.FindElement("Root"); root != nil {
171172
b := root.Bounds()
172-
x = b.W / 2
173-
y = b.H / 2
173+
x = b.Dx() / 2
174+
y = b.Dy() / 2
174175
} else {
175176
x, y = 20, 20
176177
}
@@ -264,10 +265,9 @@ func watchSettings(app *BackgroundApp) {
264265
}
265266
}
266267

267-
func setAppBackground(app *BackgroundApp, color graphics.Color) {
268-
internal := app.InternalApp()
269-
if internal != nil {
270-
internal.SetBackground(color)
268+
func setAppBackground(app *BackgroundApp, color color.NRGBA) {
269+
if app != nil {
270+
app.App.SetBackground(color)
271271
}
272272
}
273273

@@ -386,10 +386,10 @@ func flagProvided(name string) bool {
386386
return false
387387
}
388388

389-
func parseHexColor(value string) (graphics.Color, error) {
389+
func parseHexColor(value string) (color.NRGBA, error) {
390390
v := strings.TrimPrefix(strings.TrimSpace(value), "#")
391391
if len(v) != 6 && len(v) != 8 {
392-
return graphics.Color{}, fmt.Errorf("invalid --color %q: expected RRGGBB or RRGGBBAA", value)
392+
return color.NRGBA{}, fmt.Errorf("invalid --color %q: expected RRGGBB or RRGGBBAA", value)
393393
}
394394
var parsed uint32
395395
for _, ch := range v {
@@ -402,13 +402,13 @@ func parseHexColor(value string) (graphics.Color, error) {
402402
case ch >= 'A' && ch <= 'F':
403403
parsed += uint32(ch-'A') + 10
404404
default:
405-
return graphics.Color{}, fmt.Errorf("invalid --color %q: bad hex digit %q", value, string(ch))
405+
return color.NRGBA{}, fmt.Errorf("invalid --color %q: bad hex digit %q", value, string(ch))
406406
}
407407
}
408-
return graphics.NewColorHex(parsed), nil
408+
return core.NewColorHex(parsed), nil
409409
}
410410

411-
func colorToHex(c graphics.Color) string {
411+
func colorToHex(c color.NRGBA) string {
412412
return fmt.Sprintf("#%02x%02x%02x", c.R, c.G, c.B)
413413
}
414414

apps/demo/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import (
2323
"log"
2424

2525
gapp "avyos.dev/pkg/graphics/app"
26-
declapp "avyos.dev/pkg/graphics/app/decl"
2726
)
2827

2928
//go:embed ui/demo.ui
3029
var demoUI string
3130

3231
type DemoApp struct {
33-
declapp.App
32+
gapp.App
3433
clickCount int
3534
optionCursor int
3635
}

apps/dock/main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
settingsapi "avyos.dev/api/settings"
1616
"avyos.dev/pkg/appcatalog"
1717
gapp "avyos.dev/pkg/graphics/app"
18-
declapp "avyos.dev/pkg/graphics/app/decl"
1918
displaybackend "avyos.dev/pkg/graphics/backend/display"
2019
gfxicons "avyos.dev/pkg/graphics/icons"
21-
graphics "avyos.dev/pkg/graphics/input"
22-
gfxtheme "avyos.dev/pkg/graphics/theme"
20+
core "avyos.dev/pkg/graphics/pixmap"
21+
gfxtheme "avyos.dev/pkg/graphics/themes"
2322
ui "avyos.dev/pkg/graphics/widget/engine"
2423
"avyos.dev/pkg/logger"
2524
)
@@ -89,7 +88,7 @@ type menuItem struct {
8988
}
9089

9190
type TaskbarApp struct {
92-
declapp.App
91+
gapp.App
9392

9493
home string
9594
catalog []appcatalog.Entry
@@ -510,8 +509,8 @@ func (a *TaskbarApp) openGroupMenu(key string, anchor *ui.Element) {
510509
content := buildContextMenu(menuW, menuH, items)
511510

512511
b := anchor.Bounds()
513-
x := b.X + (b.W-menuW)/2
514-
y := b.Y - menuH - menuOffset
512+
x := b.Min.X + (b.Dx()-menuW)/2
513+
y := b.Min.Y - menuH - menuOffset
515514

516515
var popup *displaybackend.Popup
517516
popup = db.OpenPopup(x, y, menuW, menuH, content, func() {
@@ -747,7 +746,7 @@ func run() error {
747746
Height: taskbarHeight,
748747
Backend: db,
749748
Input: db,
750-
Background: graphics.ColorTransparent,
749+
Background: core.ColorTransparent,
751750
})
752751
if err := a.LoadString(taskbarUI, a); err != nil {
753752
log.Error("failed to load dock ui: %v", err)

apps/files/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import (
1616

1717
"avyos.dev/pkg/fs"
1818
gapp "avyos.dev/pkg/graphics/app"
19-
declapp "avyos.dev/pkg/graphics/app/decl"
20-
gfxfont "avyos.dev/pkg/graphics/font"
19+
gfxfont "avyos.dev/pkg/graphics/fonts"
2120
gfxicons "avyos.dev/pkg/graphics/icons"
2221
ui "avyos.dev/pkg/graphics/widget/engine"
2322
"avyos.dev/pkg/identity"
@@ -90,7 +89,7 @@ type driveEntry struct {
9089
}
9190

9291
type fileManager struct {
93-
declapp.App
92+
gapp.App
9493

9594
cwd string
9695
home string

apps/gallery/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
gapp "avyos.dev/pkg/graphics/app"
12-
declapp "avyos.dev/pkg/graphics/app/decl"
1312
gfxcanvas "avyos.dev/pkg/graphics/canvas"
1413
ui "avyos.dev/pkg/graphics/widget/engine"
1514
)
@@ -18,7 +17,7 @@ import (
1817
var imageViewerUI string
1918

2019
type ImageViewerApp struct {
21-
declapp.App
20+
gapp.App
2221

2322
currentPath string
2423
}

apps/launcher/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111
display "avyos.dev/api/display"
1212
"avyos.dev/pkg/appcatalog"
1313
gapp "avyos.dev/pkg/graphics/app"
14-
declapp "avyos.dev/pkg/graphics/app/decl"
1514
displaybackend "avyos.dev/pkg/graphics/backend/display"
16-
graphics "avyos.dev/pkg/graphics/input"
15+
core "avyos.dev/pkg/graphics/pixmap"
1716
ui "avyos.dev/pkg/graphics/widget/engine"
1817
"avyos.dev/pkg/logger"
1918
)
@@ -30,7 +29,7 @@ const (
3029
var log = logger.New(appMenuID)
3130

3231
type AppMenu struct {
33-
declapp.App
32+
gapp.App
3433

3534
home string
3635
catalog []appcatalog.Entry
@@ -285,7 +284,7 @@ func main() {
285284
Title: "App Menu",
286285
Backend: backend,
287286
Input: backend,
288-
Background: graphics.ColorTransparent,
287+
Background: core.ColorTransparent,
289288
})
290289
if err := app.LoadString(appMenuUI, app); err != nil {
291290
log.Error("failed to load appmenu ui: %v", err)

0 commit comments

Comments
 (0)