Downloads component now uses textarea widget to display file preview.
This commit is contained in:
@@ -3,12 +3,14 @@ import imguin/[cimgui, glfw_opengl, simple]
|
|||||||
import ../../utils/[appImGui, colors]
|
import ../../utils/[appImGui, colors]
|
||||||
import ../../../common/[types, utils]
|
import ../../../common/[types, utils]
|
||||||
import ../../core/websocket
|
import ../../core/websocket
|
||||||
|
import ../widgets/textarea
|
||||||
|
|
||||||
type
|
type
|
||||||
DownloadsComponent* = ref object of RootObj
|
DownloadsComponent* = ref object of RootObj
|
||||||
title: string
|
title: string
|
||||||
items*: seq[LootItem]
|
items*: seq[LootItem]
|
||||||
contents*: Table[string, string]
|
contents*: Table[string, string]
|
||||||
|
textarea: TextareaWidget
|
||||||
selectedIndex: int
|
selectedIndex: int
|
||||||
|
|
||||||
|
|
||||||
@@ -18,6 +20,7 @@ proc LootDownloads*(title: string): DownloadsComponent =
|
|||||||
result.items = @[]
|
result.items = @[]
|
||||||
result.contents = initTable[string, string]()
|
result.contents = initTable[string, string]()
|
||||||
result.selectedIndex = -1
|
result.selectedIndex = -1
|
||||||
|
result.textarea = Textarea(showTimestamps = false, autoScroll = false)
|
||||||
|
|
||||||
proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: WsConnection) =
|
proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: WsConnection) =
|
||||||
igBegin(component.title, showComponent, 0)
|
igBegin(component.title, showComponent, 0)
|
||||||
@@ -64,6 +67,7 @@ proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: W
|
|||||||
let isSelected = component.selectedIndex == i
|
let isSelected = component.selectedIndex == i
|
||||||
if igSelectable_Bool(item.lootId.cstring, isSelected, ImGuiSelectableFlags_SpanAllColumns.int32 or ImGuiSelectableFlags_AllowOverlap.int32, vec2(0, 0)):
|
if igSelectable_Bool(item.lootId.cstring, isSelected, ImGuiSelectableFlags_SpanAllColumns.int32 or ImGuiSelectableFlags_AllowOverlap.int32, vec2(0, 0)):
|
||||||
component.selectedIndex = i
|
component.selectedIndex = i
|
||||||
|
component.textarea.clear()
|
||||||
|
|
||||||
if igIsItemHovered(ImGuiHoveredFlags_None.int32) and igIsMouseClicked_Bool(ImGuiMouseButton_Right.int32, false):
|
if igIsItemHovered(ImGuiHoveredFlags_None.int32) and igIsMouseClicked_Bool(ImGuiMouseButton_Right.int32, false):
|
||||||
component.selectedIndex = i
|
component.selectedIndex = i
|
||||||
@@ -132,8 +136,12 @@ proc draw*(component: DownloadsComponent, showComponent: ptr bool, connection: W
|
|||||||
igSeparator()
|
igSeparator()
|
||||||
igDummy(vec2(0.0f, 5.0f))
|
igDummy(vec2(0.0f, 5.0f))
|
||||||
|
|
||||||
igTextUnformatted(component.contents[item.lootId], nil)
|
if component.textarea.isEmpty() and not component.contents[item.lootId].isEmptyOrWhitespace():
|
||||||
|
component.textarea.addItem(LOG_OUTPUT, component.contents[item.lootId])
|
||||||
|
|
||||||
|
component.textarea.draw(vec2(-1.0f, -1.0f))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
igText("Select item to preview contents")
|
igText("Select item to preview contents")
|
||||||
|
|
||||||
igEndChild()
|
igEndChild()
|
||||||
@@ -9,6 +9,7 @@ type
|
|||||||
contentDisplayed: ConsoleItems
|
contentDisplayed: ConsoleItems
|
||||||
textSelect: ptr TextSelect
|
textSelect: ptr TextSelect
|
||||||
showTimestamps: bool
|
showTimestamps: bool
|
||||||
|
autoScroll: bool
|
||||||
|
|
||||||
# Text highlighting
|
# Text highlighting
|
||||||
proc getText(item: ConsoleItem): cstring =
|
proc getText(item: ConsoleItem): cstring =
|
||||||
@@ -32,7 +33,7 @@ proc getLineAtIndex(i: csize_t, data: pointer, outLen: ptr csize_t): cstring {.c
|
|||||||
outLen[] = line.len.csize_t
|
outLen[] = line.len.csize_t
|
||||||
return line
|
return line
|
||||||
|
|
||||||
proc Textarea*(showTimestamps: bool = true): TextareaWidget =
|
proc Textarea*(showTimestamps: bool = true, autoScroll: bool = true): TextareaWidget =
|
||||||
result = new TextareaWidget
|
result = new TextareaWidget
|
||||||
result.content = new ConsoleItems
|
result.content = new ConsoleItems
|
||||||
result.content.items = @[]
|
result.content.items = @[]
|
||||||
@@ -40,6 +41,7 @@ proc Textarea*(showTimestamps: bool = true): TextareaWidget =
|
|||||||
result.contentDisplayed.items = @[]
|
result.contentDisplayed.items = @[]
|
||||||
result.textSelect = textselect_create(getLineAtIndex, getNumLines, cast[pointer](result.contentDisplayed), 0)
|
result.textSelect = textselect_create(getLineAtIndex, getNumLines, cast[pointer](result.contentDisplayed), 0)
|
||||||
result.showTimestamps = showTimestamps
|
result.showTimestamps = showTimestamps
|
||||||
|
result.autoScroll = autoScroll
|
||||||
|
|
||||||
# API to add new content entry
|
# API to add new content entry
|
||||||
proc addItem*(component: TextareaWidget, itemType: LogType, data: string, timestamp: string = now().format("dd-MM-yyyy HH:mm:ss")) =
|
proc addItem*(component: TextareaWidget, itemType: LogType, data: string, timestamp: string = now().format("dd-MM-yyyy HH:mm:ss")) =
|
||||||
@@ -55,9 +57,11 @@ proc clear*(component: TextareaWidget) =
|
|||||||
component.contentDisplayed.items.setLen(0)
|
component.contentDisplayed.items.setLen(0)
|
||||||
component.textSelect.textselect_clear_selection()
|
component.textSelect.textselect_clear_selection()
|
||||||
|
|
||||||
|
proc isEmpty*(component: TextareaWidget): bool =
|
||||||
|
return component.content.items.len() <= 0
|
||||||
|
|
||||||
# Drawing
|
# Drawing
|
||||||
proc print(component: TextareaWidget, item: ConsoleItem) =
|
proc print(component: TextareaWidget, item: ConsoleItem) =
|
||||||
|
|
||||||
if item.itemType != LOG_OUTPUT and component.showTimestamps:
|
if item.itemType != LOG_OUTPUT and component.showTimestamps:
|
||||||
igTextColored(GRAY, "[" & item.timestamp & "]", nil)
|
igTextColored(GRAY, "[" & item.timestamp & "]", nil)
|
||||||
igSameLine(0.0f, 0.0f)
|
igSameLine(0.0f, 0.0f)
|
||||||
@@ -103,8 +107,9 @@ proc draw*(component: TextareaWidget, size: ImVec2, filter: ptr ImGuiTextFilter
|
|||||||
component.print(item)
|
component.print(item)
|
||||||
|
|
||||||
# Auto-scroll to bottom
|
# Auto-scroll to bottom
|
||||||
if igGetScrollY() >= igGetScrollMaxY():
|
if component.autoScroll:
|
||||||
igSetScrollHereY(1.0f)
|
if igGetScrollY() >= igGetScrollMaxY():
|
||||||
|
igSetScrollHereY(1.0f)
|
||||||
|
|
||||||
component.textSelect.textselect_update()
|
component.textSelect.textselect_update()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user