Skip to content

Commit

Permalink
refactor(developer): update app context as well as cached context for…
Browse files Browse the repository at this point in the history
… KMN keyboard debugger

Fixes #10214.
  • Loading branch information
mcdurdin committed Jan 22, 2024
1 parent fe88d04 commit aee59db
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface
Keyman.System.Debug.DebugCore,
Keyman.System.Debug.DebugEvent,
Keyman.System.Debug.DebugUIStatus,
Keyman.System.Debug.DebugUtils,
Keyman.System.KeymanCore,
Keyman.System.KeymanCoreDebug,
UframeTextEditor,
Expand Down Expand Up @@ -115,8 +116,6 @@ TfrmLdmlKeyboardDebug = class(TTikeForm)
function HandleMemoKeydown(var Message: TMessage): Boolean;
procedure SetCurrentEvent(Value: Integer);
procedure Run;
function GetContextFromMemo(
IncludeMarkers: Boolean): TArray<km_core_context_item>;

protected
function GetHelpTopic: string; override;
Expand Down Expand Up @@ -291,52 +290,6 @@ procedure TfrmLdmlKeyboardDebug.memoMessage(Sender: TObject; var Message: TMessa
end;
end;

function TfrmLdmlKeyboardDebug.GetContextFromMemo(IncludeMarkers: Boolean): TArray<km_core_context_item>;
var
n, i: Integer;
ch: Char;
dk: TDeadKeyInfo;
begin
n := 0;
SetLength(Result, Length(memo.Text)+1);
i := 1;
while i <= memo.SelStart + memo.SelLength do
begin
ch := memo.Text[i];
if Uni_IsSurrogate1(ch) and (i < Length(memo.Text)) and
Uni_IsSurrogate2(memo.Text[i+1]) then
begin
Result[n]._type := KM_CORE_CT_CHAR;
Result[n].character := Uni_SurrogateToUTF32(ch, memo.Text[i+1]);
Inc(i);
end
else if Ord(ch) = $FFFC then
begin
if IncludeMarkers then
begin
Result[n]._type := KM_CORE_CT_MARKER;
dk := FDeadkeys.GetFromPosition(i-1);
Assert(Assigned(dk));
Result[n].marker := dk.Deadkey.Value;
end
else
begin
Inc(i);
Continue;
end;
end
else
begin
Result[n]._type := KM_CORE_CT_CHAR;
Result[n].character := Ord(ch);
end;
Inc(i);
Inc(n);
end;

Result[n]._type := KM_CORE_CT_END;
end;

function TfrmLdmlKeyboardDebug.SetKeyEventContext: Boolean;
var
context: pkm_core_context;
Expand All @@ -360,14 +313,14 @@ function TfrmLdmlKeyboardDebug.SetKeyEventContext: Boolean;
end;

// Set the cached context
context_items := GetContextFromMemo(True);
context_items := GetContextFromMemo(memo, FDeadkeys, True);
context := km_core_state_context(FDebugCore.State);
Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK;

if Result then
begin
// Set the app context
context_items := GetContextFromMemo(False);
context_items := GetContextFromMemo(memo, FDeadkeys, False);
context := km_core_state_app_context(FDebugCore.State);
Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK;
end;
Expand Down
47 changes: 12 additions & 35 deletions developer/src/tike/child/UfrmDebug.pas
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface
Keyman.System.Debug.DebugCore,
Keyman.System.Debug.DebugEvent,
Keyman.System.Debug.DebugUIStatus,
Keyman.System.Debug.DebugUtils,
Keyman.System.KeymanCore,
Keyman.System.KeymanCoreDebug,
UframeTextEditor,
Expand Down Expand Up @@ -373,12 +374,7 @@ function TfrmDebug.SetKeyEventContext: Boolean;
var
context: pkm_core_context;
context_items: TArray<km_core_context_item>;
n, i: Integer;
ch: Char;
dk: TDeadKeyInfo;
begin
context := km_core_state_context(FDebugCore.State);

if memo.SelLength > 0 then
begin
// When there is a selection, we'll treat it as
Expand All @@ -392,41 +388,22 @@ function TfrmDebug.SetKeyEventContext: Boolean;
// backspace in the case of backspace key, rather
// than deleting the last character of the selection
// (Keyman Core is not aware of selection).
km_core_context_clear(context);
km_core_state_context_clear(FDebugCore.State);
Exit(True);
end;

n := 0;
SetLength(context_items, Length(memo.Text)+1);
i := 1;
while i <= memo.SelStart + memo.SelLength do
// Set the cached context
context_items := GetContextFromMemo(memo, FDeadkeys, True);
context := km_core_state_context(FDebugCore.State);
Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK;

if Result then
begin
ch := memo.Text[i];
if Uni_IsSurrogate1(ch) and (i < Length(memo.Text)) and
Uni_IsSurrogate2(memo.Text[i+1]) then
begin
context_items[n]._type := KM_CORE_CT_CHAR;
context_items[n].character := Uni_SurrogateToUTF32(ch, memo.Text[i+1]);
Inc(i);
end
else if Ord(ch) = $FFFC then
begin
context_items[n]._type := KM_CORE_CT_MARKER;
dk := FDeadkeys.GetFromPosition(i-1);
Assert(Assigned(dk));
context_items[n].marker := dk.Deadkey.Value;
end
else
begin
context_items[n]._type := KM_CORE_CT_CHAR;
context_items[n].character := Ord(ch);
end;
Inc(i);
Inc(n);
// Set the app context
context_items := GetContextFromMemo(memo, FDeadkeys, False);
context := km_core_state_app_context(FDebugCore.State);
Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK;
end;

context_items[n]._type := KM_CORE_CT_END;
Result := km_core_context_set(context, @context_items[0]) = KM_CORE_STATUS_OK;
end;

function TfrmDebug.ProcessKeyEvent(var Message: TMessage): Boolean;
Expand Down
63 changes: 63 additions & 0 deletions developer/src/tike/debug/Keyman.System.Debug.DebugUtils.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
unit Keyman.System.Debug.DebugUtils;

interface

uses
debugdeadkeys,
Keyman.System.KeymanCore,
KeymanDeveloperDebuggerMemo;

function GetContextFromMemo(Memo: TKeymanDeveloperDebuggerMemo; DeadKeys: TDebugDeadkeyInfoList; IncludeMarkers: Boolean): TArray<km_core_context_item>;

implementation

uses
Unicode;

function GetContextFromMemo(Memo: TKeymanDeveloperDebuggerMemo; DeadKeys: TDebugDeadkeyInfoList; IncludeMarkers: Boolean): TArray<km_core_context_item>;
var
n, i: Integer;
ch: Char;
dk: TDeadKeyInfo;
begin
n := 0;
SetLength(Result, Length(Memo.Text)+1);
i := 1;
while i <= Memo.SelStart + Memo.SelLength do
begin
ch := Memo.Text[i];
if Uni_IsSurrogate1(ch) and (i < Length(Memo.Text)) and
Uni_IsSurrogate2(Memo.Text[i+1]) then
begin
Result[n]._type := KM_CORE_CT_CHAR;
Result[n].character := Uni_SurrogateToUTF32(ch, Memo.Text[i+1]);
Inc(i);
end
else if Ord(ch) = $FFFC then
begin
if IncludeMarkers then
begin
Result[n]._type := KM_CORE_CT_MARKER;
dk := DeadKeys.GetFromPosition(i-1);
Assert(Assigned(dk));
Result[n].marker := dk.Deadkey.Value;
end
else
begin
Inc(i);
Continue;
end;
end
else
begin
Result[n]._type := KM_CORE_CT_CHAR;
Result[n].character := Ord(ch);
end;
Inc(i);
Inc(n);
end;

Result[n]._type := KM_CORE_CT_END;
end;

end.
3 changes: 2 additions & 1 deletion developer/src/tike/tike.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ uses
Keyman.System.CopyDataHelper in 'main\Keyman.System.CopyDataHelper.pas',
Keyman.Developer.System.ProjectOwningFile in 'main\Keyman.Developer.System.ProjectOwningFile.pas',
Keyman.Developer.System.Main in 'main\Keyman.Developer.System.Main.pas',
Keyman.Developer.System.LaunchProjects in 'main\Keyman.Developer.System.LaunchProjects.pas';
Keyman.Developer.System.LaunchProjects in 'main\Keyman.Developer.System.LaunchProjects.pas',
Keyman.System.Debug.DebugUtils in 'debug\Keyman.System.Debug.DebugUtils.pas';

{$R *.RES}
{$R ICONS.RES}
Expand Down
11 changes: 6 additions & 5 deletions developer/src/tike/tike.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,17 @@
<DCCReference Include="..\kmconvert\Keyman.Developer.System.TouchLayoutToVisualKeyboardConverter.pas"/>
<DCCReference Include="project\Keyman.Developer.System.Project.kmnProjectFileAction.pas"/>
<DCCReference Include="project\Keyman.Developer.System.Project.kpsProjectFileAction.pas"/>
<DCCReference Include="project\Keyman.Developer.UI.Project.UfrmNewProjectParameters.pas">
<Form>frmNewProjectParameters</Form>
<FormType>dfm</FormType>
</DCCReference>
<DCCReference Include="project\Keyman.Developer.UI.Project.UfrmNewLDMLKeyboardProjectParameters.pas">
<Form>frmNewLDMLKeyboardProjectParameters</Form>
</DCCReference>
<DCCReference Include="project\Keyman.Developer.UI.Project.UfrmNewProject.pas">
<Form>frmNewProject</Form>
</DCCReference>
<DCCReference Include="..\kmconvert\Keyman.Developer.System.KeyboardProjectTemplate.pas"/>
<DCCReference Include="..\kmconvert\Keyman.Developer.System.LdmlKeyboardProjectTemplate.pas"/>
<DCCReference Include="main\Keyman.Developer.UI.ImportWindowsKeyboardDialogManager.pas"/>
<DCCReference Include="..\kmconvert\Keyman.Developer.System.ImportWindowsKeyboard.pas"/>
Expand Down Expand Up @@ -578,11 +583,7 @@
<DCCReference Include="main\Keyman.Developer.System.ProjectOwningFile.pas"/>
<DCCReference Include="main\Keyman.Developer.System.Main.pas"/>
<DCCReference Include="main\Keyman.Developer.System.LaunchProjects.pas"/>
<DCCReference Include="project\Keyman.Developer.UI.Project.UfrmNewProjectParameters.pas">
<Form>frmNewProjectParameters</Form>
<FormType>dfm</FormType>
</DCCReference>
<DCCReference Include="..\kmconvert\Keyman.Developer.System.KeyboardProjectTemplate.pas"/>
<DCCReference Include="debug\Keyman.System.Debug.DebugUtils.pas"/>
<None Include="Profiling\AQtimeModule1.aqt"/>
<BuildConfiguration Include="Debug">
<Key>Cfg_2</Key>
Expand Down

0 comments on commit aee59db

Please sign in to comment.