Skip to content

Commit

Permalink
Update DnD_uifigure.m
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangruili committed Oct 17, 2023
1 parent f1560bb commit 9546cda
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions DnD_uifigure.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ function DnD_uifigure(target, dropFcn)

jsStr = char(strjoin([ ... % webwindow accepts only char at least for R2020b
% """use strict"";"
"let uiFileDnD = {rects: [], index: 0, lastOver: 0,"
"let uiFileDnD = {rects: [], lastOver: 0,"
" data: {ctrlKey: false, shiftKey: false, index: 0},"
" button: dojo.query('.mwPushButton').find(b => b.textContent==='%s')};"
"document.ondragenter = (e) => { // prevent default before firing ondragover"
" e.dataTransfer.dropEffect = 'none';"
Expand All @@ -50,22 +51,22 @@ function DnD_uifigure(target, dropFcn)
"document.ondragover = (e) => {"
" e.returnValue = false; // preventDefault & stopPropagation"
" let now = new Date().getTime();"
" if (now > uiFileDnD.lastOver+16) {"
" uiFileDnD.lastOver = now;"
" let x = e.clientX+1, y = document.body.clientHeight-e.clientY;"
" for (let i = uiFileDnD.rects.length-1; i >= 0; i--) {"
" let p = uiFileDnD.rects[i]; // [left bottom width height]"
" if (x>=p[0] && y>=p[1] && x<p[0]+p[2] && y<p[1]+p[3]) {"
" uiFileDnD.index = i; // target index in rects"
" return; // keep OS default dropEffect"
" };"
" if (now < uiFileDnD.lastOver+16) { return; }"
" uiFileDnD.lastOver = now;"
" let x = e.clientX+1, y = document.body.clientHeight-e.clientY;"
" for (let i = uiFileDnD.rects.length-1; i >= 0; i--) {"
" let p = uiFileDnD.rects[i]; // [left bottom width height]"
" if (x>=p[0] && y>=p[1] && x<p[0]+p[2] && y<p[1]+p[3]) {"
" uiFileDnD.data.index = i; // target index in rects"
" return; // keep OS default dropEffect"
" };"
" };"
" e.dataTransfer.dropEffect = 'none'; // disable drop"
"};"
"document.ondrop = (e) => {"
" e.returnValue = false;"
" uiFileDnD.data = {ctrlKey: e.ctrlKey, shiftKey: e.shiftKey};"
" uiFileDnD.data.ctrlKey = e.ctrlKey;"
" uiFileDnD.data.shiftKey = e.shiftKey;"
" uiFileDnD.button.click(); // fire Matlab callback"
"};" ], newline));
drawnow; ww.executeJS(sprintf(jsStr, h.Text));
Expand All @@ -84,7 +85,7 @@ function dragEnter(ww, names, h)
function drop(h, ~, ww)
dat = jsondecode(ww.executeJS('uiFileDnD.data'));
dat.names = h.Text;
args = [h.UserData(jsondecode(ww.executeJS('uiFileDnD.index'))+1,:) dat];
args = [h.UserData(dat.index+1,:) rmfield(dat, 'index')];
if iscell(args{1}), args = [args{1}(1) args(2:3) args{1}(2:end)]; end
feval(args{:});
%%

0 comments on commit 9546cda

Please sign in to comment.