master
Dustin Swan 2 weeks ago
parent 373ec8b36b
commit abca3eb397
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -133,5 +133,3 @@ any = f list \ fold (acc x \ or acc (f x)) False list;
# all : (a \ Bool) \ List a \ Bool
all = f list \ fold (acc x \ and acc (f x)) True list;
# split : String \ String \ String

@ -98,12 +98,17 @@ box = config \
]
};
insertChar = text pos char \
textInput = config \
defaults = { onSubmit = _ \ noOp };
c = { ...defaults, ...config };
_ = debug "c" c;
insertChar = text pos char \
before = slice text 0 pos;
after = slice text pos (len text);
before & char & after;
deleteChar = text pos \
deleteChar = text pos \
(pos == 0
| True \ text
| False \
@ -111,7 +116,7 @@ deleteChar = text pos \
after = slice text pos (len text);
before & after));
calcScrollOffset = text cursorPos scrollOffset inputWidth \
calcScrollOffset = text cursorPos scrollOffset inputWidth \
textBeforeCursor = slice text 0 cursorPos;
cursorX = ui.measureText textBeforeCursor;
(cursorX < scrollOffset
@ -121,7 +126,7 @@ calcScrollOffset = text cursorPos scrollOffset inputWidth \
| True \ cursorX - inputWidth + 20
| False \ scrollOffset));
findPosHelper = text targetX index \
findPosHelper = text targetX index \
(index >= len text)
| True \ len text
| False \ (
@ -133,60 +138,65 @@ findPosHelper = text targetX index \
| False \ findPosHelper text targetX (index + 1))
);
findCursorPos = text clickX scrollOffset inputPadding \
findCursorPos = text clickX scrollOffset inputPadding \
adjustedX = clickX + scrollOffset - inputPadding;
findPosHelper text adjustedX 0;
textInput = config \ ui.stateful {
key = config.key,
ui.stateful {
key = c.key,
focusable = True,
autoFocus = config.initialFocus,
autoFocus = c.initialFocus,
# init : State
init = {
text = config.initialValue,
focused = config.initialFocus,
text = c.initialValue,
focused = c.initialFocus,
cursorPos = 0,
scrollOffset = 0
},
# update : State \ Event \ State
update = state event \ event
| Key { key = c, printable = True, meta = False, ctrl = False } \ (
newText = insertChar state.text state.cursorPos c;
| Key { key = key, printable = True, meta = False, ctrl = False } \ (
newText = insertChar state.text state.cursorPos key;
newCursorPos = state.cursorPos + 1;
newScroll = calcScrollOffset newText newCursorPos state.scrollOffset config.w;
newScroll = calcScrollOffset newText newCursorPos state.scrollOffset c.w;
newState = state.{ text = newText, cursorPos = newCursorPos, scrollOffset = newScroll };
{ state = newState, emit = [config.onChange newText] })
{ state = newState, emit = [c.onChange newText] })
| Key { key = "ArrowLeft" } \ (
newCursorPos = max 0 (state.cursorPos - 1);
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset config.w;
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset c.w;
newState = state.{ text = state.text, cursorPos = newCursorPos, scrollOffset = newScroll };
{ state = newState, emit = [] })
| Key { key = "ArrowRight" } \ (
newCursorPos = min (len state.text) (state.cursorPos + 1);
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset config.w;
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset c.w;
newState = state.{ text = state.text, cursorPos = newCursorPos, scrollOffset = newScroll };
{ state = newState, emit = [] })
| Key { key = "Backspace" } \ (
newText = deleteChar state.text state.cursorPos;
newCursorPos = max 0 (state.cursorPos - 1);
newScroll = calcScrollOffset newText newCursorPos state.scrollOffset config.w;
newScroll = calcScrollOffset newText newCursorPos state.scrollOffset c.w;
newState = state.{ text = newText, cursorPos = newCursorPos, scrollOffset = newScroll };
{ state = newState, emit = [config.onChange newText] })
{ state = newState, emit = [c.onChange newText] })
| Key { key = "Enter" } \ (
# newState = state.{ text = "", cursorPos = 0, scrollOffset = 0 };
# do we blank it out here? wish parent could decide..
{ state = state, emit = [c.onSubmit state.text] })
| Clicked coords \ (
newCursorPos = findCursorPos state.text coords.x state.scrollOffset 8;
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset config.w;
newScroll = calcScrollOffset state.text newCursorPos state.scrollOffset c.w;
newState = state.{ text = state.text, cursorPos = newCursorPos, scrollOffset = newScroll };
{ state = newState, emit = [] })
| Focused \ { state = state.{ focused = True }, emit = [] }
| Blurred \ { state = state.{ focused = False }, emit = [] }
# | Key { key = k } \ { state = state, emit = [\ config.onKeyDown k ] }
# | Key { key = k } \ { state = state, emit = [\ c.onKeyDown k ] }
| _ \ { state = state, emit = [] },
view = state \
@ -195,26 +205,26 @@ textInput = config \ ui.stateful {
padding = 8;
ui.clip {
w = config.w,
h = config.h,
w = c.w,
h = c.h,
child = ui.stack {
children = [
ui.rect { w = config.w, h = config.h, color = config.backgroundColor, radius = 0 },
ui.rect { w = c.w, h = c.h, color = c.backgroundColor, radius = 0 },
ui.positioned {
x = 8 - state.scrollOffset,
y = 0,
child = ui.positioned { x = 0, y = 12, child = ui.text { content = state.text, color = config.color } }
child = ui.positioned { x = 0, y = 12, child = ui.text { content = state.text, color = c.color } }
},
(state.focused
| True \ ui.positioned {
x = 8 + cursorX - state.scrollOffset,
y = 8,
child = ui.rect { w = 2, h = 24, color = config.color }
child = ui.rect { w = 2, h = 24, color = c.color }
}
| _ \ empty)
]
}
}
};
};

@ -175,6 +175,14 @@ os =
| Defined name \ openWindow name (_ \ inspector { name = name })
| Err msg \ openWindow "Error" (_ \ ui.text { content = msg, color = "red" });
handleFocusLeftEvent = state \
newIndex = max 0 (osState.wm.focusedIndex - 1);
{ state = state, emit = [focusWindow newIndex] };
handleFocusRightEvent = state \
newIndex = min (len osState.windows - 1) (osState.wm.focusedIndex + 1);
{ state = state, emit = [focusWindow newIndex] };
ui.stateful {
key = "os",
autoFocus = True,
@ -184,16 +192,10 @@ os =
update = state event \ event
| Key { key = "p", meta = True } \
{ state = state, emit = [openPalette] }
| Key { key = "ArrowLeft", meta = True } \
(
newIndex = max 0 (osState.wm.focusedIndex - 1);
{ state = state, emit = [focusWindow newIndex] }
)
| Key { key = "ArrowRight", meta = True } \
(
newIndex = min (len osState.windows - 1) (osState.wm.focusedIndex + 1);
{ state = state, emit = [focusWindow newIndex] }
)
| Key { key = "ArrowLeft", meta = True } \ handleFocusLeftEvent state
| Key { key = "h", meta = True } \ handleFocusLeftEvent state
| Key { key = "ArrowRight", meta = True } \ handleFocusRightEvent state
| Key { key = "l", meta = True } \ handleFocusRightEvent state
| Key { key = "d", meta = True } \
{ state = state, emit = [closeFocusedWindow 0] }
| Key { key = "f", meta = True } \

Loading…
Cancel
Save