mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-24 13:48:53 +08:00
shadow type for Button
This commit is contained in:
parent
b43d89d851
commit
c44c556439
@ -7,7 +7,7 @@ Command Line User Interface (Console UI inspired by TurboVision) with built-in t
|
||||
|
||||
|
||||
## Current version
|
||||
The current version is 1.2.0. Please see details in [changelog](./changelog).
|
||||
The current version is 1.2.1. Please see details in [changelog](./changelog).
|
||||
|
||||
## Applications that uses the library
|
||||
* Terminal FB2 reader(termfb2): https://github.com/VladimirMarkelov/termfb2
|
||||
|
33
button.go
33
button.go
@ -17,6 +17,7 @@ type Button struct {
|
||||
shadowColor term.Attribute
|
||||
bgActive term.Attribute
|
||||
pressed int32
|
||||
shadowType ButtonShadow
|
||||
onClick func(Event)
|
||||
}
|
||||
|
||||
@ -90,8 +91,24 @@ func (b *Button) Draw() {
|
||||
SetTextColor(fg)
|
||||
shift, text := AlignColorizedText(b.title, w-1, b.align)
|
||||
if b.isPressed() == 0 {
|
||||
SetBackColor(shadow)
|
||||
FillRect(x+1, y+1, w-1, h-1, ' ')
|
||||
switch b.shadowType {
|
||||
case ShadowFull:
|
||||
SetBackColor(shadow)
|
||||
FillRect(x+1, y+h-1, w-1, 1, ' ')
|
||||
FillRect(x+w-1, y+1, 1, h-1, ' ')
|
||||
case ShadowHalf:
|
||||
parts := []rune(SysObject(ObjButton))
|
||||
var bottomCh, rightCh rune
|
||||
if len(parts) < 2 {
|
||||
bottomCh, rightCh = '▀', '█'
|
||||
} else {
|
||||
bottomCh, rightCh = parts[0], parts[1]
|
||||
}
|
||||
SetTextColor(shadow)
|
||||
FillRect(x+1, y+h-1, w-1, 1, bottomCh)
|
||||
FillRect(x+w-1, y+1, 1, h-2, rightCh)
|
||||
}
|
||||
SetTextColor(fg)
|
||||
SetBackColor(bg)
|
||||
FillRect(x, y, w-1, h-1, ' ')
|
||||
DrawText(x+shift, y+dy, text)
|
||||
@ -167,3 +184,15 @@ func (b *Button) ProcessEvent(event Event) bool {
|
||||
func (b *Button) OnClick(fn func(Event)) {
|
||||
b.onClick = fn
|
||||
}
|
||||
|
||||
// ShadowType returns type of a show the button drops
|
||||
func (b *Button) ShadowType() ButtonShadow {
|
||||
return b.shadowType
|
||||
}
|
||||
|
||||
// SetShadowType changes the shadow the button drops
|
||||
func (b *Button) SetShadowType(sh ButtonShadow) {
|
||||
b.mtx.Lock()
|
||||
b.shadowType = sh
|
||||
b.mtx.Unlock()
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
2019-05-10 - version 1.2.1
|
||||
2019-06-10 - version 1.2.1
|
||||
[*] Trim only trailing spaces when loading text into TextView
|
||||
[+] New Button property ShadowType = ShadowFull(default), ShadowHalf(bottom
|
||||
part of shadow uses half-block), ShadowNone(no shadow at all)
|
||||
|
||||
2019-05-10 - version 1.2.0
|
||||
[*] Dual license: MIT or BSD-2-Clause
|
||||
|
13
consts.go
13
consts.go
@ -47,6 +47,8 @@ type (
|
||||
// SortOrder is a way of sorting rows in TableView
|
||||
SortOrder int
|
||||
DragType int
|
||||
// ButtonShadow is a type of shadow that a Button drops
|
||||
ButtonShadow int
|
||||
)
|
||||
|
||||
const (
|
||||
@ -175,6 +177,7 @@ const (
|
||||
ObjBarChart = "BarChart"
|
||||
ObjSparkChart = "SparkChart"
|
||||
ObjTableView = "TableView"
|
||||
ObjButton = "Button"
|
||||
)
|
||||
|
||||
// Available color identifiers that can be used in themes
|
||||
@ -349,3 +352,13 @@ const (
|
||||
// Sort descending
|
||||
SortDesc
|
||||
)
|
||||
|
||||
// ButtonShadow constants
|
||||
const (
|
||||
// Basic button shadow
|
||||
ShadowFull ButtonShadow = iota
|
||||
// Half-symbols are used to draw a shadow that makes it slimmer
|
||||
ShadowHalf
|
||||
// No shadow and button turns a flat one
|
||||
ShadowNone
|
||||
)
|
||||
|
69
demos/buttons/buttons.go
Normal file
69
demos/buttons/buttons.go
Normal file
@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
ui "github.com/VladimirMarkelov/clui"
|
||||
)
|
||||
|
||||
func createView() {
|
||||
view := ui.AddWindow(0, 0, 10, 7, "Button` Demo")
|
||||
view.SetTitleButtons(ui.ButtonMaximize | ui.ButtonClose)
|
||||
|
||||
frmViews := ui.CreateFrame(view, 8, 5, ui.BorderNone, ui.Fixed)
|
||||
frmViews.SetPack(ui.Horizontal)
|
||||
frmFull := ui.CreateFrame(frmViews, 8, 5, ui.BorderThin, ui.Fixed)
|
||||
frmFull.SetPack(ui.Vertical)
|
||||
frmFull.SetTitle("Full")
|
||||
frmHalf := ui.CreateFrame(frmViews, 8, 5, ui.BorderThin, ui.Fixed)
|
||||
frmHalf.SetPack(ui.Vertical)
|
||||
frmHalf.SetTitle("Half")
|
||||
frmNone := ui.CreateFrame(frmViews, 8, 5, ui.BorderThin, ui.Fixed)
|
||||
frmNone.SetPack(ui.Vertical)
|
||||
frmNone.SetTitle("None")
|
||||
|
||||
btnF1 := ui.CreateButton(frmFull, ui.AutoSize, 4, "First", ui.Fixed)
|
||||
btnF2 := ui.CreateButton(frmFull, ui.AutoSize, 4, "Second", ui.Fixed)
|
||||
btnF3 := ui.CreateButton(frmFull, ui.AutoSize, 4, "Quit", ui.Fixed)
|
||||
btnF1.SetShadowType(ui.ShadowFull)
|
||||
btnF2.SetShadowType(ui.ShadowFull)
|
||||
btnF3.SetShadowType(ui.ShadowFull)
|
||||
btnH1 := ui.CreateButton(frmHalf, ui.AutoSize, 4, "First", ui.Fixed)
|
||||
btnH2 := ui.CreateButton(frmHalf, ui.AutoSize, 4, "Second", ui.Fixed)
|
||||
btnH3 := ui.CreateButton(frmHalf, ui.AutoSize, 4, "Quit", ui.Fixed)
|
||||
btnH1.SetShadowType(ui.ShadowHalf)
|
||||
btnH2.SetShadowType(ui.ShadowHalf)
|
||||
btnH3.SetShadowType(ui.ShadowHalf)
|
||||
btnN1 := ui.CreateButton(frmNone, ui.AutoSize, 4, "First", ui.Fixed)
|
||||
btnN2 := ui.CreateButton(frmNone, ui.AutoSize, 4, "Second", ui.Fixed)
|
||||
btnN3 := ui.CreateButton(frmNone, ui.AutoSize, 4, "Quit", ui.Fixed)
|
||||
btnN1.SetShadowType(ui.ShadowNone)
|
||||
btnN2.SetShadowType(ui.ShadowNone)
|
||||
btnN3.SetShadowType(ui.ShadowNone)
|
||||
|
||||
btnF3.OnClick(func(ev ui.Event) {
|
||||
go ui.Stop()
|
||||
})
|
||||
btnH3.OnClick(func(ev ui.Event) {
|
||||
go ui.Stop()
|
||||
})
|
||||
btnN3.OnClick(func(ev ui.Event) {
|
||||
go ui.Stop()
|
||||
})
|
||||
|
||||
ui.ActivateControl(view, btnF1)
|
||||
}
|
||||
|
||||
func mainLoop() {
|
||||
// Every application must create a single Composer and
|
||||
// call its intialize method
|
||||
ui.InitLibrary()
|
||||
defer ui.DeinitLibrary()
|
||||
|
||||
createView()
|
||||
|
||||
// start event processing loop - the main core of the library
|
||||
ui.MainLoop()
|
||||
}
|
||||
|
||||
func main() {
|
||||
mainLoop()
|
||||
}
|
1
theme.go
1
theme.go
@ -145,6 +145,7 @@ func ThemeReset() {
|
||||
defTheme.objects[ObjBarChart] = "█─│┌┐└┘┬┴├┤┼"
|
||||
defTheme.objects[ObjSparkChart] = "█"
|
||||
defTheme.objects[ObjTableView] = "─│┼▼▲"
|
||||
defTheme.objects[ObjButton] = "▀█"
|
||||
|
||||
defTheme.colors[ColorDisabledText] = ColorBlackBold
|
||||
defTheme.colors[ColorDisabledBack] = ColorWhite
|
||||
|
Loading…
x
Reference in New Issue
Block a user