2023-03-21 10:26:48 -06:00
|
|
|
package fileElements
|
|
|
|
|
2023-03-23 12:11:42 -06:00
|
|
|
import "time"
|
2023-03-21 10:26:48 -06:00
|
|
|
import "io/fs"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "image"
|
2023-03-23 12:11:42 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-03-21 10:26:48 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-03-21 10:26:48 -06:00
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// File displays an interactive visual representation of a file within any
|
|
|
|
// file system.
|
2023-03-21 10:26:48 -06:00
|
|
|
type File struct {
|
2023-03-21 16:03:31 -06:00
|
|
|
*core.Core
|
|
|
|
*core.FocusableCore
|
|
|
|
core core.CoreControl
|
|
|
|
focusableControl core.FocusableCoreControl
|
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-03-23 12:11:42 -06:00
|
|
|
|
|
|
|
lastClick time.Time
|
|
|
|
pressed bool
|
2023-03-21 16:03:31 -06:00
|
|
|
iconID theme.Icon
|
2023-03-21 10:26:48 -06:00
|
|
|
filesystem fs.StatFS
|
2023-03-21 16:03:31 -06:00
|
|
|
location string
|
2023-03-23 12:11:42 -06:00
|
|
|
selected bool
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
onChoose func ()
|
2023-03-21 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// NewFile creates a new file element. If within is nil, it will use the OS file
|
|
|
|
// system
|
2023-03-21 10:26:48 -06:00
|
|
|
func NewFile (
|
|
|
|
location string,
|
|
|
|
within fs.StatFS,
|
|
|
|
) (
|
|
|
|
element *File,
|
|
|
|
err error,
|
|
|
|
) {
|
2023-03-21 16:03:31 -06:00
|
|
|
element = &File { }
|
|
|
|
element.theme.Case = theme.C("files", "file")
|
|
|
|
element.Core, element.core = core.NewCore(element, element.drawAll)
|
|
|
|
element.FocusableCore,
|
|
|
|
element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)
|
2023-03-21 10:26:48 -06:00
|
|
|
err = element.SetLocation(location, within)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// Location returns the file's location and filesystem.
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *File) Location () (string, fs.StatFS) {
|
|
|
|
return element.location, element.filesystem
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// SetLocation sets the file's location and filesystem. If within is nil, it
|
|
|
|
// will use the OS file system.
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *File) SetLocation (
|
|
|
|
location string,
|
|
|
|
within fs.StatFS,
|
|
|
|
) error {
|
|
|
|
if within == nil {
|
|
|
|
within = defaultFS { }
|
|
|
|
}
|
|
|
|
element.location = location
|
|
|
|
element.filesystem = within
|
|
|
|
return element.Update()
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// Update refreshes the element to match the file it represents.
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *File) Update () error {
|
2023-03-21 16:03:31 -06:00
|
|
|
element.iconID = theme.IconError
|
2023-03-21 10:26:48 -06:00
|
|
|
info, err := element.filesystem.Stat(element.location)
|
|
|
|
if err != nil { return err }
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// TODO: choose icon based on file mime type
|
2023-03-21 10:26:48 -06:00
|
|
|
if info.IsDir() {
|
2023-03-21 16:03:31 -06:00
|
|
|
element.iconID = theme.IconDirectory
|
2023-03-21 10:26:48 -06:00
|
|
|
} else {
|
2023-03-21 16:03:31 -06:00
|
|
|
element.iconID = theme.IconFile
|
2023-03-21 10:26:48 -06:00
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.drawAndPush()
|
2023-03-21 10:26:48 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:11:42 -06:00
|
|
|
func (element *File) Selected () bool {
|
|
|
|
return element.selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) SetSelected (selected bool) {
|
|
|
|
if element.selected == selected { return }
|
|
|
|
element.selected = selected
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
if key == input.KeyEnter {
|
|
|
|
element.pressed = true
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter && element.pressed {
|
|
|
|
element.pressed = false
|
|
|
|
element.drawAndPush()
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
if element.onChoose != nil {
|
|
|
|
element.onChoose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *File) OnChoose (callback func ()) {
|
|
|
|
element.onChoose = callback
|
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
|
2023-03-23 12:11:42 -06:00
|
|
|
func (element *File) HandleMouseDown (x, y int, button input.Button) {
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
if !element.Focused() { element.Focus() }
|
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
element.pressed = true
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) HandleMouseUp (x, y int, button input.Button) {
|
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
element.pressed = false
|
|
|
|
within := image.Point { x, y }.
|
|
|
|
In(element.Bounds())
|
|
|
|
if time.Since(element.lastClick) < time.Second / 2 {
|
|
|
|
if element.Enabled() && within && element.onChoose != nil {
|
|
|
|
element.onChoose()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.lastClick = time.Now()
|
|
|
|
}
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
func (element *File) state () theme.State {
|
|
|
|
return theme.State {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Focused: element.Focused(),
|
2023-03-23 12:11:42 -06:00
|
|
|
Pressed: element.pressed,
|
|
|
|
On: element.selected,
|
2023-03-21 16:03:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) icon () artist.Icon {
|
|
|
|
return element.theme.Icon(element.iconID, theme.IconSizeLarge)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) updateMinimumSize () {
|
|
|
|
padding := element.theme.Padding(theme.PatternButton)
|
|
|
|
icon := element.icon()
|
|
|
|
if icon == nil {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
padding.Horizontal(),
|
|
|
|
padding.Vertical())
|
|
|
|
} else {
|
|
|
|
bounds := padding.Inverse().Apply(icon.Bounds())
|
|
|
|
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) drawAndPush () {
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.drawAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *File) drawAll () {
|
|
|
|
// background
|
|
|
|
state := element.state()
|
|
|
|
bounds := element.Bounds()
|
2023-03-23 12:11:42 -06:00
|
|
|
sink := element.theme.Sink(theme.PatternButton)
|
2023-03-21 16:03:31 -06:00
|
|
|
element.theme.
|
|
|
|
Pattern(theme.PatternButton, state).
|
|
|
|
Draw(element.core, bounds)
|
|
|
|
|
|
|
|
// icon
|
|
|
|
icon := element.icon()
|
|
|
|
if icon != nil {
|
|
|
|
iconBounds := icon.Bounds()
|
|
|
|
offset := image.Pt (
|
|
|
|
(bounds.Dx() - iconBounds.Dx()) / 2,
|
|
|
|
(bounds.Dy() - iconBounds.Dy()) / 2)
|
2023-03-23 12:11:42 -06:00
|
|
|
if element.pressed {
|
|
|
|
offset = offset.Add(sink)
|
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
icon.Draw (
|
|
|
|
element.core,
|
|
|
|
element.theme.Color (
|
|
|
|
theme.ColorForeground, state),
|
|
|
|
bounds.Min.Add(offset))
|
|
|
|
}
|
|
|
|
}
|