From 9e08b3d02ef3bc0710edd9243a4bac8e792e341b Mon Sep 17 00:00:00 2001 From: Kevin Hogeland Date: Sun, 22 Oct 2017 21:19:52 -0700 Subject: [PATCH] Remove unnecessary threads --- ui/event.nim | 29 +-------- ui/listpane.nim | 9 ++- ui/searchbox.nim | 15 +++-- ui/ui.nim | 158 +++++++++++++++++------------------------------ ui/uistate.nim | 14 ----- 5 files changed, 69 insertions(+), 156 deletions(-) delete mode 100644 ui/uistate.nim diff --git a/ui/event.nim b/ui/event.nim index 299bde2..fba87aa 100644 --- a/ui/event.nim +++ b/ui/event.nim @@ -1,32 +1,15 @@ import libmpdclient, nimbox, - uistate, ../mpd/album -type OmnimStateEventType* = enum - NimboxEvent - SearchResults - StateClose - type OmnimSearchEventType* = enum UpdateQuery ExecuteSearch SearchClose -type OmnimDrawEventType* = enum - UpdateState - Redraw - DrawClose - - -type OmnimStateEvent* = ref object - case kind*: OmnimStateEventType - of NimboxEvent: - nimboxEvent*: Event - of SearchResults: - songs*: seq[ptr mpd_song] - else: discard +type OmnimSearchResultsEvent* = ref object + songs*: seq[ptr mpd_song] type OmnimSearchEvent* = ref object case kind*: OmnimSearchEventType @@ -34,11 +17,3 @@ type OmnimSearchEvent* = ref object query*: string else: discard -type OmnimDrawEvent* = ref object - case kind*: OmnimDrawEventType - of UpdateState: - searchBoxState*: SearchBoxState - listState*: ListState - else: discard - - diff --git a/ui/listpane.nim b/ui/listpane.nim index 13fea60..38fb2b8 100644 --- a/ui/listpane.nim +++ b/ui/listpane.nim @@ -1,6 +1,5 @@ import pane, - uistate, ../mpd/album, ../mpd/mpdeither @@ -82,11 +81,11 @@ proc setValues*(this: MpdListPane, values: seq[ptr mpd_song]) = this.albumValues = albums_for(values) this.updateView() -proc drawTo*(state: ListState, nb: Nimbox, x, y: int) = - for i, t in state.currentView: +proc drawTo*(this: MpdListPane, nb: Nimbox) = + for i, t in this.currentView: let style: Style = - if i == state.cursor: + if i == this.cursor: styReverse else: styNone - nb.print(x, y + i, t, clrDefault, clrDefault, style) + nb.print(this.x, this.y + i, t, clrDefault, clrDefault, style) diff --git a/ui/searchbox.nim b/ui/searchbox.nim index 3f03f50..13acd42 100644 --- a/ui/searchbox.nim +++ b/ui/searchbox.nim @@ -4,8 +4,7 @@ import unicode import - pane, - uistate + pane import nimbox @@ -28,11 +27,11 @@ proc handleSearchBoxInput*(this: SearchBox, input: Rune) = proc initSearchBox*(x, y, w, h: int): SearchBox = return SearchBox(x: x, y: y, width: w, height: h, contents: @[]) -proc drawTo*(state: SearchBoxState, nb: Nimbox, x, y: int) = - let line = PREFIX & $state.query - let lineLen = len(state.query) + PREFIX_LEN +proc drawTo*(this: SearchBox, nb: Nimbox) = + let line = PREFIX & $this.contents + let lineLen = len(this.contents) + PREFIX_LEN var afterCursor = spaces(nb.width-lineLen-1) - nb.print(x, y, line, clrDefault, clrDefault, styUnderline) - nb.print(x + lineLen, y, " ", clrDefault, clrDefault, styReverse) - nb.print(x + lineLen + 1, y, afterCursor, clrDefault, clrDefault, styUnderline) + nb.print(this.x, this.y, line, clrDefault, clrDefault, styUnderline) + nb.print(this.x + lineLen, this.y, " ", clrDefault, clrDefault, styReverse) + nb.print(this.x + lineLen + 1, this.y, afterCursor, clrDefault, clrDefault, styUnderline) diff --git a/ui/ui.nim b/ui/ui.nim index b36cc1c..bc8a082 100644 --- a/ui/ui.nim +++ b/ui/ui.nim @@ -13,7 +13,6 @@ import import event, - uistate, pane, listpane, searchbox @@ -28,28 +27,8 @@ import const MINIMUM_CHARACTERS = 3 var nb: Nimbox -var drawChannel: Channel[OmnimDrawEvent] +var resultsChannel: Channel[OmnimSearchResultsEvent] var searchChannel: Channel[OmnimSearchEvent] -var stateChannel: Channel[OmnimStateEvent] - -proc drawLoop() {.thread.} = - var searchBoxState = initSearchBoxState() - var listState = initListState() - while true: - let event: OmnimDrawEvent = drawChannel.recv() - case event.kind: - of UpdateState: - if event.searchBoxState != nil: - searchBoxState = event.searchBoxState - if event.listState != nil: - listState = event.listState - of Redraw: - nb.clear() - searchBoxState.drawTo(nb, 0, 0) - listState.drawTo(nb, 0, 1) - nb.present() - of DrawClose: - return proc searchLoop(myCtl: MpdCtl) {.thread.} = var nextQuery = "" @@ -62,103 +41,78 @@ proc searchLoop(myCtl: MpdCtl) {.thread.} = if len(nextQuery) >= MINIMUM_CHARACTERS: let query = nextQuery nextQuery = "" - stateChannel.send(OmnimStateEvent(kind: SearchResults, songs: myCtl.searchSongs(query))) + resultsChannel.send(OmnimSearchResultsEvent(songs: myCtl.searchSongs(query))) of SearchClose: return -proc stateLoop(myCtl: MpdCtl, songMode: bool) {.thread.} = - var searchBox = initSearchBox(0, 0, nb.width, 1) - var listPane = initMpdListPane(0, 1, nb.width, nb.height-1, songMode) - while true: - let event: OmnimStateEvent = stateChannel.recv() - var drawThreadUpdate = OmnimDrawEvent(kind: UpdateState) - var searchThreadUpdate = OmnimSearchEvent(kind: UpdateQuery) - - proc updateSearchBoxState() = - drawThreadUpdate.searchBoxState = SearchBoxState(query: searchBox.contents) - searchThreadUpdate.query = $searchBox.contents - - proc updateListState() = - drawThreadUpdate.listState = ListState(currentView: listPane.currentView, cursor: listPane.cursor) - - case event.kind: - of NimboxEvent: - let nbEvent = event.nimboxEvent - case nbEvent.kind: - of EventType.Key: - if (nbEvent.sym == Symbol.Down): - listPane.down() - updateListState() - elif (nbEvent.sym == Symbol.Up): - listPane.up() - updateListState() - elif nbEvent.sym == Symbol.Enter: - listPane.getCurrentValue().map(proc(value: MpdEither) = myCtl.replaceAndPlay(value)) - elif nbEvent.ch == Rune('2') and nbEvent.mods.contains(Modifier.Ctrl): - listPane.getCurrentValue().map(proc(value: MpdEither) = myCtl.enqueue(value)) - listPane.down() - updateListState() - elif nbEvent.ch == Rune('Z') and nbEvent.mods.contains(Modifier.Ctrl): - listPane.toggleSongMode() - updateListState() - elif nbEvent.sym == Symbol.Character: - searchBox.handleSearchBoxInput(nbEvent.ch) - updateSearchBoxState() - elif nbEvent.sym == Symbol.Space: - searchBox.handleSearchBoxInput(Rune(' ')) - updateSearchBoxState() - elif nbEvent.sym == Symbol.Backspace: - searchBox.backspace() - updateSearchBoxState() - of EventType.Resize: - listPane.resizePane(nbEvent.w, nbEvent.h-1) - searchBox.resizePane(nbEvent.w, 1) - updateListState() - updateSearchBoxState() - else: - discard - of SearchResults: - listPane.setValues(event.songs) - updateListState() - of StateClose: - return - - if drawThreadUpdate.listState != nil or drawThreadUpdate.searchBoxState != nil: - drawChannel.send(drawThreadUpdate) - - if searchThreadUpdate.query != nil: - searchChannel.send(searchThreadUpdate) - proc stopUi*() = nb.shutdown() - drawChannel.send(OmnimDrawEvent(kind: DrawClose)) searchChannel.send(OmnimSearchEvent(kind: SearchClose)) - stateChannel.send(OmnimStateEvent(kind: StateClose)) - close drawChannel + close resultsChannel close searchChannel - close stateChannel proc startUi*(mpd: MpdCtl, songMode: bool = false) = nb = newNimbox() - open drawChannel + open resultsChannel open searchChannel - open stateChannel - spawn drawLoop() spawn searchLoop(mpd) - spawn stateLoop(mpd, songMode) + defer: stopUi() var lastTime = 0.0 + var searchBox = initSearchBox(0, 0, nb.width, 1) + var listPane = initMpdListPane(0, 1, nb.width, nb.height-1, songMode) while true: + + nb.clear() + listPane.drawTo(nb) + searchBox.drawTo(nb) + nb.present() + let thisTime = epochTime() - drawChannel.send(OmnimDrawEvent(kind: Redraw)) - if lastTime + 0.5 < thisTime: # Execute search every half-second + if lastTime + 0.1 < thisTime: # Execute search every 100ms lastTime = thisTime searchChannel.send(OmnimSearchEvent(kind: ExecuteSearch)) - let event = nb.peekEvent(10) - case event.kind: + + var searchThreadUpdate = OmnimSearchEvent(kind: UpdateQuery) + + proc updateSearchMessage() = searchThreadUpdate.query = $searchBox.contents + + let nbEvent = nb.peekEvent(10) + case nbEvent.kind: of EventType.Key: - if event.sym == Symbol.Escape: - stopUi() + if nbEvent.sym == Symbol.Escape: return - else: discard - stateChannel.send(OmnimStateEvent(kind: NimboxEvent, nimboxEvent: event)) + if (nbEvent.sym == Symbol.Down): + listPane.down() + elif (nbEvent.sym == Symbol.Up): + listPane.up() + elif nbEvent.sym == Symbol.Enter: + listPane.getCurrentValue().map(proc(value: MpdEither) = mpd.replaceAndPlay(value)) + elif nbEvent.ch == Rune('2') and nbEvent.mods.contains(Modifier.Ctrl): + listPane.getCurrentValue().map(proc(value: MpdEither) = mpd.enqueue(value)) + listPane.down() + elif nbEvent.ch == Rune('Z') and nbEvent.mods.contains(Modifier.Ctrl): + listPane.toggleSongMode() + elif nbEvent.sym == Symbol.Character: + searchBox.handleSearchBoxInput(nbEvent.ch) + updateSearchMessage() + elif nbEvent.sym == Symbol.Space: + searchBox.handleSearchBoxInput(Rune(' ')) + updateSearchMessage() + elif nbEvent.sym == Symbol.Backspace: + searchBox.backspace() + updateSearchMessage() + of EventType.Resize: + listPane.resizePane(nbEvent.w, nbEvent.h-1) + searchBox.resizePane(nbEvent.w, 1) + else: + discard + + if len(searchBox.contents) < MINIMUM_CHARACTERS: + listPane.setValues(@[]) + + if resultsChannel.peek() > 0: + listPane.setValues(resultsChannel.recv().songs) + + if searchThreadUpdate.query != nil: + searchChannel.send(searchThreadUpdate) diff --git a/ui/uistate.nim b/ui/uistate.nim deleted file mode 100644 index 436e75b..0000000 --- a/ui/uistate.nim +++ /dev/null @@ -1,14 +0,0 @@ -import - unicode - -type SearchBoxState* = ref object - query*: seq[Rune] - -type ListState* = ref object - currentView*: seq[string] - cursor*: int - -proc initSearchBoxState*(): SearchBoxState = - return SearchBoxState(query: @[]) -proc initListState*(): ListState = - return ListState(currentView: @[], cursor: 0)