2023-03-21 10:26:48 -06:00
|
|
|
package fileElements
|
|
|
|
|
|
|
|
import "io/fs"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "image"
|
2023-03-21 10:26:48 -06:00
|
|
|
import "path/filepath"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-03-23 12:11:42 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-03-23 13:38:51 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
2023-03-21 16:03:31 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-03-21 10:26:48 -06:00
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
type fileLayoutEntry struct {
|
|
|
|
*File
|
|
|
|
fs.DirEntry
|
|
|
|
Bounds image.Rectangle
|
2023-03-23 13:38:51 -06:00
|
|
|
Drawer textdraw.Drawer
|
|
|
|
TextPoint image.Point
|
2023-03-21 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:37:44 -06:00
|
|
|
type historyEntry struct {
|
|
|
|
location string
|
|
|
|
filesystem ReadDirStatFS
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// DirectoryView displays a list of files within a particular directory and
|
|
|
|
// file system.
|
2023-03-21 10:26:48 -06:00
|
|
|
type DirectoryView struct {
|
2023-03-21 16:03:31 -06:00
|
|
|
*core.Core
|
|
|
|
*core.Propagator
|
|
|
|
core core.CoreControl
|
|
|
|
|
|
|
|
children []fileLayoutEntry
|
|
|
|
scroll image.Point
|
|
|
|
contentBounds image.Rectangle
|
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
|
|
|
|
|
|
|
onScrollBoundsChange func ()
|
2023-03-21 10:26:48 -06:00
|
|
|
|
2023-03-23 12:37:44 -06:00
|
|
|
history []historyEntry
|
|
|
|
historyIndex int
|
2023-03-21 10:26:48 -06:00
|
|
|
onChoose func (file string)
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// NewDirectoryView creates a new directory view. If within is nil, it will use
|
|
|
|
// the OS file system.
|
2023-03-21 10:26:48 -06:00
|
|
|
func NewDirectoryView (
|
|
|
|
location string,
|
|
|
|
within ReadDirStatFS,
|
|
|
|
) (
|
|
|
|
element *DirectoryView,
|
|
|
|
err error,
|
|
|
|
) {
|
2023-03-21 16:03:31 -06:00
|
|
|
element = &DirectoryView { }
|
|
|
|
element.theme.Case = theme.C("files", "directoryView")
|
|
|
|
element.Core, element.core = core.NewCore(element, element.redoAll)
|
|
|
|
element.Propagator = core.NewPropagator(element, element.core)
|
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 directory's location and filesystem.
|
2023-03-23 12:37:44 -06:00
|
|
|
func (element *DirectoryView) Location () (string, ReadDirStatFS) {
|
|
|
|
if len(element.history) < 1 { return "", nil }
|
|
|
|
current := element.history[element.historyIndex]
|
|
|
|
return current.location, current.filesystem
|
2023-03-21 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// SetLocation sets the directory'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 *DirectoryView) SetLocation (
|
|
|
|
location string,
|
|
|
|
within ReadDirStatFS,
|
|
|
|
) error {
|
|
|
|
if within == nil {
|
|
|
|
within = defaultFS { }
|
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
element.scroll = image.Point { }
|
2023-03-23 12:37:44 -06:00
|
|
|
|
|
|
|
if element.history != nil {
|
|
|
|
element.historyIndex ++
|
|
|
|
}
|
|
|
|
element.history = append (
|
|
|
|
element.history[:element.historyIndex],
|
|
|
|
historyEntry { location, within })
|
2023-03-21 10:26:48 -06:00
|
|
|
return element.Update()
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:37:44 -06:00
|
|
|
// Backward goes back a directory in history
|
|
|
|
func (element *DirectoryView) Backward () (bool, error) {
|
|
|
|
if element.historyIndex > 1 {
|
|
|
|
element.historyIndex --
|
|
|
|
return true, element.Update()
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward goes forward a directory in history
|
|
|
|
func (element *DirectoryView) Forward () (bool, error) {
|
|
|
|
if element.historyIndex < len(element.history) - 1 {
|
|
|
|
element.historyIndex ++
|
|
|
|
return true, element.Update()
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// Update refreshes the directory's contents.
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *DirectoryView) Update () error {
|
2023-03-23 12:37:44 -06:00
|
|
|
location, filesystem := element.Location()
|
|
|
|
entries, err := filesystem.ReadDir(location)
|
2023-03-21 10:26:48 -06:00
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// disown all entries
|
|
|
|
for _, file := range element.children {
|
|
|
|
file.DrawTo(nil, image.Rectangle { }, nil)
|
|
|
|
file.SetParent(nil)
|
|
|
|
|
|
|
|
if file.Focused() {
|
|
|
|
file.HandleUnfocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.children = make([]fileLayoutEntry, len(entries))
|
2023-03-21 10:26:48 -06:00
|
|
|
for index, entry := range entries {
|
2023-03-23 12:37:44 -06:00
|
|
|
filePath := filepath.Join(location, entry.Name())
|
|
|
|
file, err := NewFile(filePath, filesystem)
|
2023-03-23 12:11:42 -06:00
|
|
|
if err != nil { continue }
|
2023-03-21 16:03:31 -06:00
|
|
|
file.SetParent(element)
|
2023-03-23 12:11:42 -06:00
|
|
|
file.OnChoose (func () {
|
2023-03-21 16:03:31 -06:00
|
|
|
if element.onChoose != nil {
|
|
|
|
element.onChoose(filePath)
|
|
|
|
}
|
|
|
|
})
|
2023-03-23 12:11:42 -06:00
|
|
|
element.children[index].File = file
|
|
|
|
element.children[index].DirEntry = entry
|
2023-03-23 13:38:51 -06:00
|
|
|
element.children[index].Drawer.SetFace (element.theme.FontFace(
|
|
|
|
theme.FontStyleRegular,
|
|
|
|
theme.FontSizeNormal))
|
|
|
|
element.children[index].Drawer.SetText([]rune(entry.Name()))
|
|
|
|
element.children[index].Drawer.SetAlign(textdraw.AlignCenter)
|
2023-03-21 10:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:45:46 -06:00
|
|
|
if element.core.HasImage() {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
2023-03-21 10:26:48 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
// OnChoose sets a function to be called when the user double-clicks a file or
|
|
|
|
// sub-directory within the directory view.
|
2023-03-21 10:26:48 -06:00
|
|
|
func (element *DirectoryView) OnChoose (callback func (file string)) {
|
|
|
|
element.onChoose = callback
|
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
|
|
|
|
// CountChildren returns the amount of children contained within this element.
|
|
|
|
func (element *DirectoryView) CountChildren () (count int) {
|
|
|
|
return len(element.children)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Child returns the child at the specified index. If the index is out of
|
|
|
|
// bounds, this method will return nil.
|
|
|
|
func (element *DirectoryView) Child (index int) (child elements.Element) {
|
|
|
|
if index < 0 || index > len(element.children) { return }
|
|
|
|
return element.children[index].File
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:11:42 -06:00
|
|
|
func (element *DirectoryView) HandleMouseDown (x, y int, button input.Button) {
|
|
|
|
var file *File
|
|
|
|
for _, entry := range element.children {
|
|
|
|
if image.Pt(x, y).In(entry.Bounds) {
|
|
|
|
file = entry.File
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if file != nil {
|
|
|
|
file.SetSelected(!file.Selected())
|
|
|
|
}
|
|
|
|
element.Propagator.HandleMouseDown(x, y, button)
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:03:31 -06:00
|
|
|
func (element *DirectoryView) redoAll () {
|
|
|
|
if !element.core.HasImage() { return }
|
|
|
|
|
|
|
|
// do a layout
|
|
|
|
element.doLayout()
|
|
|
|
|
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if element.scroll.Y > maxScrollHeight {
|
|
|
|
element.scroll.Y = maxScrollHeight
|
|
|
|
element.doLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw a background
|
|
|
|
rocks := make([]image.Rectangle, len(element.children))
|
|
|
|
for index, entry := range element.children {
|
|
|
|
rocks[index] = entry.Bounds
|
|
|
|
}
|
|
|
|
pattern := element.theme.Pattern (
|
|
|
|
theme.PatternPinboard,
|
|
|
|
theme.State { })
|
|
|
|
artist.DrawShatter(element.core, pattern, element.Bounds(), rocks...)
|
|
|
|
|
|
|
|
element.partition()
|
|
|
|
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
|
|
|
|
parent.NotifyScrollBoundsChange(element)
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
2023-03-23 13:38:51 -06:00
|
|
|
|
|
|
|
// draw labels
|
|
|
|
foreground := element.theme.Color(theme.ColorForeground, theme.State { })
|
|
|
|
for _, entry := range element.children {
|
|
|
|
entry.Drawer.Draw(element.core, foreground, entry.TextPoint)
|
|
|
|
}
|
2023-03-21 16:03:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DirectoryView) partition () {
|
|
|
|
for _, entry := range element.children {
|
|
|
|
entry.DrawTo(nil, entry.Bounds, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut our canvas up and give peices to child elements
|
|
|
|
for _, entry := range element.children {
|
|
|
|
if entry.Bounds.Overlaps(element.Bounds()) {
|
|
|
|
entry.DrawTo (
|
|
|
|
canvas.Cut(element.core, entry.Bounds),
|
|
|
|
entry.Bounds, func (region image.Rectangle) {
|
|
|
|
element.core.DamageRegion(region)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyMinimumSizeChange notifies the container that the minimum size of a
|
|
|
|
// child element has changed.
|
|
|
|
func (element *DirectoryView) NotifyMinimumSizeChange (child elements.Element) {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *DirectoryView) SetTheme (new theme.Theme) {
|
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
|
|
|
element.Propagator.SetTheme(new)
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *DirectoryView) SetConfig (new config.Config) {
|
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.Propagator.SetConfig(new)
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
func (element *DirectoryView) ScrollContentBounds () image.Rectangle {
|
|
|
|
return element.contentBounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's
|
|
|
|
// viewport relative to ScrollBounds.
|
|
|
|
func (element *DirectoryView) ScrollViewportBounds () image.Rectangle {
|
|
|
|
padding := element.theme.Padding(theme.PatternPinboard)
|
|
|
|
bounds := padding.Apply(element.Bounds())
|
|
|
|
bounds = bounds.Sub(bounds.Min).Add(element.scroll)
|
|
|
|
return bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
func (element *DirectoryView) ScrollTo (position image.Point) {
|
|
|
|
if position.Y < 0 {
|
|
|
|
position.Y = 0
|
|
|
|
}
|
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if position.Y > maxScrollHeight {
|
|
|
|
position.Y = maxScrollHeight
|
|
|
|
}
|
|
|
|
element.scroll = position
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnScrollBoundsChange sets a function to be called when the element's viewport
|
|
|
|
// bounds, content bounds, or scroll axes change.
|
|
|
|
func (element *DirectoryView) OnScrollBoundsChange (callback func ()) {
|
|
|
|
element.onScrollBoundsChange = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
func (element *DirectoryView) ScrollAxes () (horizontal, vertical bool) {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DirectoryView) maxScrollHeight () (height int) {
|
|
|
|
padding := element.theme.Padding(theme.PatternSunken)
|
|
|
|
viewportHeight := element.Bounds().Dy() - padding.Vertical()
|
|
|
|
height = element.contentBounds.Dy() - viewportHeight
|
|
|
|
if height < 0 { height = 0 }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DirectoryView) doLayout () {
|
|
|
|
margin := element.theme.Margin(theme.PatternPinboard)
|
|
|
|
padding := element.theme.Padding(theme.PatternPinboard)
|
|
|
|
bounds := padding.Apply(element.Bounds())
|
|
|
|
element.contentBounds = image.Rectangle { }
|
|
|
|
|
|
|
|
beginningOfRow := true
|
|
|
|
dot := bounds.Min.Sub(element.scroll)
|
2023-03-23 13:38:51 -06:00
|
|
|
rowHeight := 0
|
2023-03-21 16:03:31 -06:00
|
|
|
for index, entry := range element.children {
|
|
|
|
width, height := entry.MinimumSize()
|
|
|
|
|
|
|
|
if dot.X + width > bounds.Max.X {
|
|
|
|
dot.X = bounds.Min.Sub(element.scroll).X
|
2023-03-23 13:38:51 -06:00
|
|
|
dot.Y += rowHeight
|
2023-03-21 16:03:31 -06:00
|
|
|
if index > 1 {
|
|
|
|
dot.Y += margin.Y
|
|
|
|
}
|
|
|
|
beginningOfRow = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if beginningOfRow {
|
|
|
|
beginningOfRow = false
|
|
|
|
} else {
|
|
|
|
dot.X += margin.X
|
|
|
|
}
|
|
|
|
|
2023-03-23 13:38:51 -06:00
|
|
|
entry.Drawer.SetMaxWidth(width)
|
|
|
|
bounds := image.Rect(dot.X, dot.Y, dot.X + width, dot.Y + height)
|
|
|
|
entry.Bounds = bounds
|
|
|
|
|
|
|
|
drawerHeight := entry.Drawer.ReccomendedHeightFor(width)
|
|
|
|
entry.TextPoint =
|
|
|
|
image.Pt(bounds.Min.X, bounds.Max.Y + margin.Y).
|
|
|
|
Sub(entry.Drawer.LayoutBounds().Min)
|
|
|
|
bounds.Max.Y += margin.Y + drawerHeight
|
|
|
|
height += margin.Y + drawerHeight
|
|
|
|
if rowHeight < height {
|
|
|
|
rowHeight = height
|
|
|
|
}
|
|
|
|
|
|
|
|
element.contentBounds = element.contentBounds.Union(bounds)
|
2023-03-21 16:03:31 -06:00
|
|
|
element.children[index] = entry
|
|
|
|
dot.X += width
|
|
|
|
}
|
|
|
|
|
|
|
|
element.contentBounds =
|
|
|
|
element.contentBounds.Sub(element.contentBounds.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DirectoryView) updateMinimumSize () {
|
|
|
|
padding := element.theme.Padding(theme.PatternPinboard)
|
|
|
|
minimumWidth := 0
|
|
|
|
for _, entry := range element.children {
|
|
|
|
width, _ := entry.MinimumSize()
|
|
|
|
if width > minimumWidth {
|
|
|
|
minimumWidth = width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
minimumWidth + padding.Horizontal(),
|
|
|
|
padding.Vertical())
|
|
|
|
}
|