Skip to content

Commit

Permalink
pkg/graphics: optimized fbdev backend and added multiple widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
itsManjeet committed Mar 9, 2025
1 parent 3d22261 commit 666d486
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 148 deletions.
8 changes: 8 additions & 0 deletions .idea/dictionaries/project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions cmd/compositor/desktop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2025 Manjeet Singh <itsmanjeet1998@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main

import (
"image"
"image/color"

"rlxos.dev/pkg/graphics/canvas"
"rlxos.dev/pkg/graphics/event"
"rlxos.dev/pkg/graphics/widget"
)

type Desktop struct {
widget.BaseWidget

Background color.Color
Panel Panel
windows []widget.Widget
}

func NewDesktop() *Desktop {
d := &Desktop{
Background: color.Black,
windows: []widget.Widget{},
}

return d
}

func (d *Desktop) AddWindow(w widget.Widget) {
d.windows = append(d.windows, w)
}

func (d *Desktop) SetBounds(rect image.Rectangle) {
d.BaseWidget.SetBounds(rect)
d.Panel.SetBounds(rect)
}

func (d *Desktop) Draw(c canvas.Canvas) {
c.SetColor(d.Background)
c.FillRect(d.Bounds)

for _, w := range d.windows {
w.Draw(c)
}

d.Panel.Draw(c)
}

func (d *Desktop) Update(e event.Event) {
d.Panel.Update(e)
}
27 changes: 2 additions & 25 deletions cmd/compositor/main.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
package main

import (
"image/color"
"log"

"rlxos.dev/pkg/graphics/app"
"rlxos.dev/pkg/graphics/driver/fbdev"
"rlxos.dev/pkg/graphics/widget"
)

func main() {
if err := app.Run(&widget.Container{
Orientation: widget.Horizontal,
Spacing: 10,
Children: []widget.Widget{
&widget.Button{
BackgroundColor: color.RGBA{R: 33, G: 33, B: 33, A: 255},
HoverColor: color.RGBA{97, 97, 97, 255},
OnClick: func() {
log.Println("Clicked 1")
},
},
&widget.Button{
BackgroundColor: color.RGBA{R: 88, G: 88, B: 88, A: 255},
HoverColor: color.RGBA{97, 97, 97, 255},
OnClick: func() {
log.Println("Clicked 2")
},
},
},
}, &fbdev.Driver{
Device: "/dev/fb0",
}); err != nil {
desktop := NewDesktop()
if err := app.Run(desktop); err != nil {
log.Fatal(err)
}
}
77 changes: 77 additions & 0 deletions cmd/compositor/panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2025 Manjeet Singh <itsmanjeet1998@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package main

import (
"image"
"image/color"

"rlxos.dev/pkg/graphics/canvas"
"rlxos.dev/pkg/graphics/event"
"rlxos.dev/pkg/graphics/widget"
)

type PanelPosition int

const (
PanelPositionTop PanelPosition = iota
PanelPositionBottom
)

type Panel struct {
widget.BaseWidget

Position PanelPosition
StartButton widget.Widget
BackgroundColor color.Color
Color color.Color
Padding int
Size int
}

func NewPanel(position PanelPosition) *Panel {
p := &Panel{
Position: position,
StartButton: &widget.Button{
Label: "Start",
},
}

return p
}

func (p *Panel) SetBounds(rect image.Rectangle) {
switch p.Position {
case PanelPositionBottom:
p.BaseWidget.SetBounds(image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, p.Size))
case PanelPositionTop:
p.BaseWidget.SetBounds(image.Rect(rect.Min.X, rect.Max.Y-p.Size, rect.Max.X, rect.Max.Y))
}

p.StartButton.SetBounds(image.Rect(rect.Min.X, rect.Min.Y, p.Size-p.Padding, p.Size-p.Padding))
}

func (p *Panel) Draw(c canvas.Canvas) {
c.SetColor(p.BackgroundColor)
c.FillRect(p.Bounds)

p.StartButton.Draw(c)
}
func (p *Panel) Update(e event.Event) {
p.StartButton.Update(e)
}
10 changes: 8 additions & 2 deletions pkg/graphics/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ package app
import (
"fmt"
"log"
"time"

"rlxos.dev/pkg/graphics/driver"
"rlxos.dev/pkg/graphics/event"
"rlxos.dev/pkg/graphics/widget"
)

func Run(w widget.Widget, drivers ...driver.Driver) error {
if drivers == nil {
if len(drivers) == 0 {
drivers = supportedDrivers
}

Expand Down Expand Up @@ -55,7 +56,8 @@ func Run(w widget.Widget, drivers ...driver.Driver) error {

running := true
for running {
for _, ev := range selectedDriver.PollEvent() {
events := selectedDriver.PollEvent()
for _, ev := range events {
switch ev := ev.(type) {
case error:
log.Println("ERROR", err)
Expand All @@ -70,6 +72,10 @@ func Run(w widget.Widget, drivers ...driver.Driver) error {
if w.IsDamaged() {
surface.Update()
}

if len(events) == 0 {
time.Sleep(10 * time.Millisecond)
}
}

return nil
Expand Down
Loading

0 comments on commit 666d486

Please sign in to comment.