Skip to content

Commit 972df9b

Browse files
committed
fix: fix reopening or focusing scratchpad: only for electron
fixing a regression, and fixing #329
1 parent ff401a6 commit 972df9b

File tree

8 files changed

+52
-37
lines changed

8 files changed

+52
-37
lines changed

src/frontend/event_helpers.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ proc successMessage*(api: MediatorWithSubscribers, text: cstring) =
6363
let notification =
6464
makeNotification(NotificationKind.NotificationSuccess, text)
6565
api.showNotification(notification)
66+
67+
proc openValueInScratchpad*(api: MediatorWithSubscribers, arg: ValueWithExpression) =
68+
api.emit(InternalAddToScratchpad, arg)

src/frontend/middleware.nim

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import std / [jsffi, jsconsole]
22
import .. / common / ct_event
3-
import types
3+
import types, lib
44
import communication, dap
55
import event_helpers
66

77
# backend(dap) <-> middleware <-> view (self-contained, can be separate: 0, 1 or more components);
88

99
when not defined(ctInExtension):
10+
import utils
11+
1012
proc dapInitializationHandler() =
1113
data.dapApi.sendCtRequest(DapConfigurationDone, js{})
1214
data.dapApi.sendCtRequest(DapLaunch, js{
@@ -19,6 +21,23 @@ when not defined(ctInExtension):
1921
inc data.status.operationCount
2022
viewsApi.emit(InternalStatusUpdate, data.status)
2123

24+
proc addToScratchpadHandler*(viewsApi: MediatorWithSubscribers, value: ValueWithExpression) =
25+
# opens again or re-focuses scratchpad panel if needed
26+
let openNew = data.ui.componentMapping[Content.Scratchpad].isNil or
27+
data.ui.componentMapping[Content.Scratchpad].len == 0 or
28+
data.ui.componentMapping[Content.Scratchpad].toJs[0].isUndefined or
29+
data.ui.componentMapping[Content.Scratchpad][0].layoutItem.isNil
30+
if openNew:
31+
data.openLayoutTab(Content.Scratchpad)
32+
33+
if not data.ui.componentMapping[Content.Scratchpad][0].layoutItem.parent.isNil:
34+
data.ui.componentMapping[Content.Scratchpad][0].
35+
layoutItem.parent.setActiveContentItem(
36+
data.ui.componentMapping[Content.Scratchpad][0].layoutItem)
37+
# if there is no parent, we assume it's a visible panel
38+
# with only tab scratchpad
39+
viewsApi.emit(InternalAddToScratchpad, value)
40+
2241
proc setupMiddlewareApis*(dapApi: DapApi, viewsApi: MediatorWithSubscribers) {.exportc.}=
2342
var lastCompleteMove: MoveState = nil
2443

@@ -75,7 +94,6 @@ proc setupMiddlewareApis*(dapApi: DapApi, viewsApi: MediatorWithSubscribers) {.e
7594
# if not dapApi.completeMoveFunction.isNil:
7695
# dapApi.completeMoveFunction(dapApi.editor, value, dapApi)
7796
# )
78-
viewsApi.subscribe(InternalAddToScratchpad, proc(kind: CtEventKind, value: ValueWithExpression, sub: Subscriber) = viewsApi.emit(InternalAddToScratchpad, value))
7997
viewsApi.subscribe(InternalAddToScratchpadFromExpression, proc(kind: CtEventKind, value: cstring, sub: Subscriber) = viewsApi.emit(InternalAddToScratchpadFromExpression, value))
8098

8199
when not defined(ctInExtension):
@@ -84,6 +102,15 @@ proc setupMiddlewareApis*(dapApi: DapApi, viewsApi: MediatorWithSubscribers) {.e
84102
newOperationHandler(viewsApi, value)
85103
)
86104

105+
viewsApi.subscribe(InternalAddToScratchpad, proc(kind: CtEventKind, value: ValueWithExpression, sub: Subscriber) =
106+
addToScratchpadHandler(viewsApi, value))
107+
else:
108+
# TODO: also in extension: opening again/focusing scratchpad and
109+
# sending to it, maybe using a handler that we pass to setupMiddlewareApis ?
110+
# or a global function?
111+
viewsApi.subscribe(InternalAddToScratchpad, proc(kind: CtEventKind, value: ValueWithExpression, sub: Subscriber) = viewsApi.emit(InternalAddToScratchpad, value))
112+
113+
87114
viewsApi.subscribe(CtNotification, proc(kind: CtEventKind, value: Notification, sub: Subscriber) = viewsApi.emit(CtNotification, value))
88115
viewsApi.subscribe(DapStepIn, proc(kind: CtEventKind, value: DapStepArguments, sub: Subscriber) = dapApi.sendCtRequest(kind, value.toJs))
89116
viewsApi.subscribe(DapStepOut, proc(kind: CtEventKind, value: DapStepArguments, sub: Subscriber) = dapApi.sendCtRequest(kind, value.toJs))

src/frontend/ui/calltrace.nim

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,9 @@ proc callView*(
623623
onclick = proc =
624624
clog fmt"calltrace: jump onclick call key " & $key
625625
self.resetValueView()
626-
# TODO: send event to middleware to change state
627-
# self.data.services.debugger.stableBusy = true
628626
self.selectedCallNumber = self.lineIndex[call.key]
629627
self.lastSelectedCallKey = call.key
630628
self.calltraceJump(call.location)
631-
# TODO: send event to middleware to change status state
632-
# inc self.data.services.debugger.operationCount
633629
self.redrawCallLines()
634630
):
635631
if key != cstring"-1 -1 -1":

src/frontend/ui/editor.nim

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ proc createContextMenuItems(self: EditorViewComponent, ev: js): seq[ContextMenuI
897897
if local.expression == expression:
898898
let baseValue = local.value
899899
addToScratchpad = ContextMenuItem(name: "Add value to scratchpad", hint: "", handler: proc(e: Event) =
900-
openValueInScratchpad((expression, baseValue))
900+
self.api.openValueInScratchpad(ValueWithExpression(expression: expression, value: baseValue))
901901
self.data.redraw())
902902
contextMenu &= addToScratchpad
903903
break
@@ -927,7 +927,10 @@ proc createContextMenuItems(self: EditorViewComponent, ev: js): seq[ContextMenuI
927927
name: &"Add {tempValue[0]} to scratchpad",
928928
hint: "",
929929
handler: proc(e: Event) =
930-
openValueInScratchpad(tempValue)
930+
self.api.openValueInScratchpad(
931+
ValueWithExpression(
932+
expression: tempValue[0],
933+
value: tempValue[1]))
931934
self.data.redraw()
932935
)
933936

@@ -950,7 +953,10 @@ proc createContextMenuItems(self: EditorViewComponent, ev: js): seq[ContextMenuI
950953
hint: "",
951954
handler: proc(e: Event) =
952955
for localValue in traceValue.locals:
953-
openValueInScratchpad(localValue)
956+
self.api.openValueInScratchpad(
957+
ValueWithExpression(
958+
expression: localValue[0],
959+
value: localValue[1]))
954960
self.data.redraw()
955961
)
956962

@@ -960,7 +966,10 @@ proc createContextMenuItems(self: EditorViewComponent, ev: js): seq[ContextMenuI
960966
name: "Add value to scratchpad",
961967
hint: "CTRL+&lt;click on value&gt;",
962968
handler: proc(e: Event) =
963-
openValueInScratchpad(traceValue.locals[0])
969+
self.api.openValueInScratchpad(
970+
ValueWithExpression(
971+
expression: traceValue.locals[0][0],
972+
value: traceValue.locals[0][1]))
964973
self.data.redraw()
965974
)
966975

src/frontend/ui/flow.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import
22
../ui_helpers, ui_imports,
33
../renderer, value, scratchpad
44

5-
import ../communication, ../../common/ct_event, ../dap
5+
import ../communication, ../../common/ct_event, ../dap, ../event_helpers
66
import strutils, os
77

88
# thank, God!
@@ -1148,7 +1148,7 @@ proc createContextMenuItems(self: FlowComponent, name: cstring, beforeValue: Val
11481148
name: "Add value to scratchpad",
11491149
hint: "CTRL+&lt;click on value&gt;",
11501150
handler: proc(e: Event) =
1151-
openValueInScratchpad((name, beforeValue))
1151+
self.api.openValueInScratchpad(ValueWithExpression(expression: name, value: beforeValue))
11521152
data.redraw()
11531153
)
11541154

@@ -1159,7 +1159,7 @@ proc createContextMenuItems(self: FlowComponent, name: cstring, beforeValue: Val
11591159
hint: "",
11601160
handler: proc(e: Event) =
11611161
for key, value in step.beforeValues:
1162-
openValueInScratchpad((key, value))
1162+
self.api.openValueInScratchpad(ValueWithExpression(expression: key, value: value))
11631163
data.redraw()
11641164
)
11651165

@@ -1323,7 +1323,7 @@ proc flowSimpleValue*(
13231323

13241324
if cast[MouseEvent](e).button == 0:
13251325
if cast[bool](e.toJs.ctrlKey):
1326-
openValueInScratchpad((name, value))
1326+
self.api.openValueInScratchpad(ValueWithExpression(expression: name, value: value))
13271327
data.redraw()
13281328
else:
13291329
self.jumpToLocalStep(stepCount)

src/frontend/ui/trace.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ proc createContextMenuItems(self: TraceComponent, ev: js): seq[ContextMenuItem]
250250
hint: "",
251251
handler: proc(e: Event) =
252252
for local in localValues:
253-
openValueInScratchpad(local)
254-
self.data.redraw()
253+
self.api.emit(InternalAddToScratchpad, ValueWithExpression(expression: local[0], value: local[1]))
255254
)
256255

257256
contextMenu &= addToScratchpad

src/frontend/ui/value.nim

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,10 +1149,7 @@ proc view(
11491149
tdiv(
11501150
class = "add-to-scratchpad-button",
11511151
onmousedown = proc(ev: Event, v: VNode) =
1152-
# TODO: Figure out a way to open panel if closed
1153-
# The logic for opening should be in the middleware later on
1154-
# openValueInScratchpad((expression, value))
1155-
self.api.emit(InternalAddToScratchpad, ValueWithExpression(expression: expression, value: value))
1152+
self.api.openValueInScratchpad(ValueWithExpression(expression: expression, value: value))
11561153
self.redraw()
11571154
):
11581155
tdiv(class = "custom-tooltip"):

src/frontend/utils.nim

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ proc asmLoad*(self: EditorService, location: types.Location): Future[Instruction
277277
# else:
278278
# name = location.functionName # using it for expanded-<firstLine>
279279
let instructions = await self.data.asyncSend("asm-load", functionLocation, $name, Instructions)
280+
280281
return instructions
281282

282283
# lowLevel: enum TODO
@@ -1254,23 +1255,6 @@ proc registerScratchpadValue*(self: ScratchpadComponent, expression: cstring, va
12541255
stateID: -1,
12551256
data: self.data))
12561257

1257-
proc openValueInScratchpad*(value: (cstring, Value)) =
1258-
if data.ui.componentMapping[Content.Scratchpad].isNil or
1259-
data.ui.componentMapping[Content.Scratchpad].len == 0 or
1260-
data.ui.componentMapping[Content.Scratchpad].toJs[0].isUndefined or
1261-
data.ui.componentMapping[Content.Scratchpad][0].layoutItem.isNil:
1262-
data.openLayoutTab(Content.Scratchpad)
1263-
1264-
if not data.ui.componentMapping[Content.Scratchpad][0].layoutItem.parent.isNil:
1265-
cast[ScratchpadComponent](
1266-
data.ui.componentMapping[Content.Scratchpad][0]).registerScratchpadValue(
1267-
value[0],
1268-
value[1])
1269-
data.ui.componentMapping[Content.Scratchpad][0].
1270-
layoutItem.parent.setActiveContentItem(
1271-
data.ui.componentMapping[Content.Scratchpad][0].layoutItem)
1272-
# if there is no parent, we assume it's a visible panel
1273-
# with only tab scratchpad
12741258

12751259
proc findTRNode*(node: js): js =
12761260
return if node.tagName.to(cstring) == cstring("TR"):

0 commit comments

Comments
 (0)