2021-03-29 15:59:14 +08:00
|
|
|
view.windowFrame = is_osx ? #extended : #solid;
|
|
|
|
|
|
|
|
var body;
|
|
|
|
var connections = [];
|
|
|
|
var show_chat = false;
|
|
|
|
|
|
|
|
class Body: Reactor.Component
|
|
|
|
{
|
|
|
|
this var cur = 0;
|
|
|
|
|
|
|
|
function this() {
|
|
|
|
body = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
2022-04-29 23:32:10 +08:00
|
|
|
if (connections.length == 0)
|
|
|
|
return <div style="size:*; margin: * 0; text-align: center;">
|
|
|
|
Waiting for new connection ...
|
|
|
|
</div>;
|
2021-03-29 15:59:14 +08:00
|
|
|
var c = connections[this.cur];
|
|
|
|
this.connection = c;
|
|
|
|
this.cid = c.id;
|
|
|
|
var auth = c.authorized;
|
|
|
|
var me = this;
|
|
|
|
var callback = function(msg) {
|
|
|
|
me.sendMsg(msg);
|
|
|
|
};
|
|
|
|
var right_style = show_chat ? "" : "display: none";
|
|
|
|
return <div .content>
|
|
|
|
<div .left-panel>
|
|
|
|
<div .icon-and-id>
|
|
|
|
<div .icon style={"background: " + string2RGB(c.name, 1)}>
|
|
|
|
{c.name[0].toUpperCase()}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div .id style="font-weight: bold; font-size: 1.2em;">{c.name}</div>
|
|
|
|
<div .id>({c.peer_id})</div>
|
2021-12-25 16:45:22 +08:00
|
|
|
<div style="margin-top: 1.2em">{translate('Connected')} {" "} <span #time>{getElaspsed(c.time)}</span></div>
|
2021-03-29 15:59:14 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div />
|
2021-12-25 16:45:22 +08:00
|
|
|
{c.is_file_transfer || c.port_forward ? "" : <div>{translate('Permissions')}</div>}
|
2021-03-29 15:59:14 +08:00
|
|
|
{c.is_file_transfer || c.port_forward ? "" : <div .permissions>
|
2021-12-25 16:45:22 +08:00
|
|
|
<div class={!c.keyboard ? "disabled" : ""} title={translate('Allow using keyboard and mouse')}><icon .keyboard /></div>
|
|
|
|
<div class={!c.clipboard ? "disabled" : ""} title={translate('Allow using clipboard')}><icon .clipboard /></div>
|
|
|
|
<div class={!c.audio ? "disabled" : ""} title={translate('Allow hearing sound')}><icon .audio /></div>
|
2022-02-15 14:46:08 +08:00
|
|
|
<div class={!c.file ? "disabled" : ""} title={translate('Allow file transfer')}><icon .file /></div>
|
2021-03-29 15:59:14 +08:00
|
|
|
</div>}
|
|
|
|
{c.port_forward ? <div>Port Forwarding: {c.port_forward}</div> : ""}
|
|
|
|
<div style="size:*"/>
|
|
|
|
<div .buttons>
|
2021-12-25 16:45:22 +08:00
|
|
|
{auth ? "" : <button .button tabindex="-1" #accept>{translate('Accept')}</button>}
|
|
|
|
{auth ? "" : <button .button tabindex="-1" .outline #dismiss>{translate('Dismiss')}</button>}
|
|
|
|
{auth ? <button .button tabindex="-1" #disconnect>{translate('Disconnect')}</button> : ""}
|
2021-03-29 15:59:14 +08:00
|
|
|
</div>
|
|
|
|
{c.is_file_transfer || c.port_forward ? "" : <div .chaticon>{svg_chat}</div>}
|
|
|
|
</div>
|
|
|
|
<div .right-panel style={right_style}>
|
|
|
|
{c.is_file_transfer || c.port_forward ? "" : <ChatBox msgs={c.msgs} callback={callback} />}
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendMsg(text) {
|
|
|
|
if (!text) return;
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.msgs.push({ name: "me", text: text, time: getNowStr()});
|
|
|
|
handler.send_msg(cid, text);
|
|
|
|
body.update();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(icon.keyboard) (e) {
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.keyboard = !connection.keyboard;
|
|
|
|
body.update();
|
|
|
|
handler.switch_permission(cid, "keyboard", connection.keyboard);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(icon.clipboard) {
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.clipboard = !connection.clipboard;
|
|
|
|
body.update();
|
|
|
|
handler.switch_permission(cid, "clipboard", connection.clipboard);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(icon.audio) {
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.audio = !connection.audio;
|
|
|
|
body.update();
|
|
|
|
handler.switch_permission(cid, "audio", connection.audio);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-15 14:46:08 +08:00
|
|
|
event click $(icon.file) {
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.file = !connection.file;
|
|
|
|
body.update();
|
|
|
|
handler.switch_permission(cid, "file", connection.file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
event click $(button#accept) {
|
|
|
|
var { cid, connection } = this;
|
|
|
|
checkClickTime(function() {
|
|
|
|
connection.authorized = true;
|
|
|
|
body.update();
|
|
|
|
handler.authorize(cid);
|
2021-12-29 00:05:52 +08:00
|
|
|
self.timer(30ms, function() {
|
|
|
|
view.windowState = View.WINDOW_MINIMIZED;
|
|
|
|
});
|
2021-03-29 15:59:14 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(button#dismiss) {
|
|
|
|
var cid = this.cid;
|
|
|
|
checkClickTime(function() {
|
|
|
|
handler.close(cid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(button#disconnect) {
|
|
|
|
var cid = this.cid;
|
|
|
|
checkClickTime(function() {
|
|
|
|
handler.close(cid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(body).content(<Body />);
|
|
|
|
|
|
|
|
var header;
|
|
|
|
|
|
|
|
class Header: Reactor.Component
|
|
|
|
{
|
|
|
|
function this() {
|
|
|
|
header = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
var me = this;
|
|
|
|
var conn = connections[body.cur];
|
|
|
|
if (conn && conn.unreaded > 0) {;
|
|
|
|
var el = me.select("#unreaded" + conn.id);
|
|
|
|
if (el) el.style.set {
|
|
|
|
display: "inline-block",
|
|
|
|
};
|
|
|
|
self.timer(300ms, function() {
|
|
|
|
conn.unreaded = 0;
|
|
|
|
var el = me.select("#unreaded" + conn.id);
|
|
|
|
if (el) el.style.set {
|
|
|
|
display: "none",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var tabs = connections.map(function(c, i) { return me.renderTab(c, i) });
|
|
|
|
return <div .tabs-wrapper><div .tabs>
|
|
|
|
{tabs}
|
|
|
|
</div>
|
|
|
|
<div .tab-arrows>
|
2022-02-09 16:06:44 +08:00
|
|
|
<span #left-arrow><</span>
|
|
|
|
<span #right-arrow>></span>
|
2021-03-29 15:59:14 +08:00
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderTab(c, i) {
|
|
|
|
var cur = body.cur;
|
|
|
|
return <div class={i == cur ? "active-tab tab" : "tab"}>
|
|
|
|
{c.name}
|
|
|
|
{c.unreaded > 0 ? <span .unreaded id={"unreaded" + c.id}>{c.unreaded}</span> : ""}
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_cur(idx) {
|
|
|
|
checkClickTime(function() {
|
|
|
|
body.cur = idx;
|
|
|
|
update();
|
|
|
|
self.timer(1ms, adjustHeader);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(div.tab) (_, me) {
|
|
|
|
var idx = me.index;
|
|
|
|
if (idx == body.cur) return;
|
|
|
|
this.update_cur(idx);
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:06:44 +08:00
|
|
|
event click $(span#left-arrow) {
|
2021-03-29 15:59:14 +08:00
|
|
|
var cur = body.cur;
|
|
|
|
if (cur == 0) return;
|
|
|
|
this.update_cur(cur - 1);
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:06:44 +08:00
|
|
|
event click $(span#right-arrow) {
|
2021-03-29 15:59:14 +08:00
|
|
|
var cur = body.cur;
|
|
|
|
if (cur == connections.length - 1) return;
|
|
|
|
this.update_cur(cur + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_osx) {
|
|
|
|
$(header).content(<Header />);
|
|
|
|
$(header).attributes["role"] = "window-caption";
|
|
|
|
} else {
|
|
|
|
$(div.window-toolbar).content(<Header />);
|
|
|
|
setWindowButontsAndIcon(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
event click $(div.chaticon) {
|
|
|
|
checkClickTime(function() {
|
|
|
|
show_chat = !show_chat;
|
2022-02-09 16:06:44 +08:00
|
|
|
adaptSizeForChat();
|
2022-04-27 01:07:00 +08:00
|
|
|
if (show_chat) {
|
|
|
|
view.focus = $(.outline-focus);
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkClickTime(callback) {
|
2021-08-10 16:01:39 +08:00
|
|
|
callback();
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:06:44 +08:00
|
|
|
function adaptSizeForChat() {
|
2021-03-29 15:59:14 +08:00
|
|
|
$(div.right-panel).style.set {
|
|
|
|
display: show_chat ? "block" : "none",
|
|
|
|
};
|
|
|
|
var (x, y, w, h) = view.box(#rectw, #border, #screen);
|
|
|
|
if (show_chat && w < 600) {
|
|
|
|
view.move(x - (600 - w), y, 600, h);
|
|
|
|
} else if (!show_chat && w > 450) {
|
|
|
|
view.move(x + (w - 300), y, 300, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
header.update();
|
|
|
|
body.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
function bring_to_top(idx=-1) {
|
|
|
|
if (view.windowState == View.WINDOW_HIDDEN || view.windowState == View.WINDOW_MINIMIZED) {
|
2021-05-02 22:55:50 +08:00
|
|
|
if (is_linux) {
|
|
|
|
view.focus = self;
|
|
|
|
} else {
|
|
|
|
view.windowState = View.WINDOW_SHOWN;
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
if (idx >= 0) body.cur = idx;
|
|
|
|
} else {
|
|
|
|
view.windowTopmost = true;
|
|
|
|
view.windowTopmost = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 14:46:08 +08:00
|
|
|
handler.addConnection = function(id, is_file_transfer, port_forward, peer_id, name, authorized, keyboard, clipboard, audio, file) {
|
2021-03-29 15:59:14 +08:00
|
|
|
var conn;
|
|
|
|
connections.map(function(c) {
|
|
|
|
if (c.id == id) conn = c;
|
|
|
|
});
|
|
|
|
if (conn) {
|
|
|
|
conn.authorized = authorized;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!name) name = "NA";
|
|
|
|
connections.push({
|
|
|
|
id: id, is_file_transfer: is_file_transfer, peer_id: peer_id,
|
|
|
|
port_forward: port_forward,
|
|
|
|
name: name, authorized: authorized, time: new Date(),
|
|
|
|
keyboard: keyboard, clipboard: clipboard, msgs: [], unreaded: 0,
|
2022-02-15 14:46:08 +08:00
|
|
|
audio: audio, file: file
|
2021-03-29 15:59:14 +08:00
|
|
|
});
|
|
|
|
body.cur = connections.length - 1;
|
|
|
|
bring_to_top();
|
|
|
|
update();
|
|
|
|
self.timer(1ms, adjustHeader);
|
|
|
|
if (authorized) {
|
|
|
|
self.timer(3s, function() {
|
|
|
|
view.windowState = View.WINDOW_MINIMIZED;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.removeConnection = function(id) {
|
|
|
|
var i = -1;
|
|
|
|
connections.map(function(c, idx) {
|
|
|
|
if (c.id == id) i = idx;
|
|
|
|
});
|
2022-04-29 19:40:53 +08:00
|
|
|
if (i < 0) return;
|
2021-03-29 15:59:14 +08:00
|
|
|
connections.splice(i, 1);
|
|
|
|
if (connections.length == 0) {
|
|
|
|
handler.exit();
|
|
|
|
} else {
|
|
|
|
if (body.cur >= i && body.cur > 0) body.cur -= 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.newMessage = function(id, text) {
|
|
|
|
var idx = -1;
|
|
|
|
connections.map(function(c, i) {
|
|
|
|
if (c.id == id) idx = i;
|
|
|
|
});
|
|
|
|
var conn = connections[idx];
|
|
|
|
if (!conn) return;
|
|
|
|
conn.msgs.push({name: conn.name, text: text, time: getNowStr()});
|
|
|
|
bring_to_top(idx);
|
2022-02-09 16:06:44 +08:00
|
|
|
if (idx == body.cur) {
|
|
|
|
var shouldAdapt = !show_chat;
|
|
|
|
show_chat = true;
|
|
|
|
if (shouldAdapt) adaptSizeForChat();
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
conn.unreaded += 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
view << event statechange {
|
|
|
|
adjustBorder();
|
|
|
|
}
|
|
|
|
|
|
|
|
function self.ready() {
|
|
|
|
adjustBorder();
|
|
|
|
var (sw, sh) = view.screenBox(#workarea, #dimension);
|
|
|
|
var w = 300;
|
|
|
|
var h = 400;
|
|
|
|
view.move(sw - w, 0, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getElaspsed(time) {
|
|
|
|
var now = new Date();
|
|
|
|
var seconds = Date.diff(time, now, #seconds);
|
|
|
|
var hours = seconds / 3600;
|
|
|
|
var days = hours / 24;
|
|
|
|
hours = hours % 24;
|
|
|
|
var minutes = seconds % 3600 / 60;
|
|
|
|
seconds = seconds % 60;
|
|
|
|
var out = String.printf("%02d:%02d:%02d", hours, minutes, seconds);
|
|
|
|
if (days > 0) {
|
|
|
|
out = String.printf("%d day%s %s", days, days > 1 ? "s" : "", out);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTime() {
|
|
|
|
self.timer(1s, function() {
|
|
|
|
var el = $(#time);
|
|
|
|
if (el) {
|
|
|
|
var c = connections[body.cur];
|
|
|
|
if (c) {
|
|
|
|
el.text = getElaspsed(c.time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateTime();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTime();
|
|
|
|
|
2022-04-29 23:32:10 +08:00
|
|
|
var tm0 = getTime();
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
function self.closing() {
|
2022-04-29 23:32:10 +08:00
|
|
|
if (connections.length == 0 && getTime() - tm0 > 30000) return true;
|
2021-03-29 15:59:14 +08:00
|
|
|
view.windowState = View.WINDOW_HIDDEN;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function adjustHeader() {
|
|
|
|
var hw = $(header).box(#width);
|
|
|
|
var tabswrapper = $(div.tabs-wrapper);
|
|
|
|
var tabs = $(div.tabs);
|
|
|
|
var arrows = $(div.tab-arrows);
|
|
|
|
if (!arrows) return;
|
|
|
|
var n = connections.length;
|
|
|
|
var wtab = 80;
|
|
|
|
var max = hw - 98;
|
|
|
|
var need_width = n * wtab + 2; // include border of active tab
|
|
|
|
if (need_width < max) {
|
|
|
|
arrows.style.set {
|
|
|
|
display: "none",
|
|
|
|
};
|
|
|
|
tabs.style.set {
|
|
|
|
width: need_width,
|
|
|
|
margin-left: 0,
|
|
|
|
};
|
|
|
|
tabswrapper.style.set {
|
|
|
|
width: need_width,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
var margin = (body.cur + 1) * wtab - max + 30;
|
|
|
|
if (margin < 0) margin = 0;
|
|
|
|
arrows.style.set {
|
|
|
|
display: "block",
|
|
|
|
};
|
|
|
|
tabs.style.set {
|
|
|
|
width: (max - 20 + margin) + 'px',
|
|
|
|
margin-left: -margin + 'px'
|
|
|
|
};
|
|
|
|
tabswrapper.style.set {
|
|
|
|
width: (max + 10) + 'px',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
view.on("size", adjustHeader);
|
|
|
|
|
2022-02-15 15:35:19 +08:00
|
|
|
// handler.addConnection(0, false, 0, "", "test1", true, false, false, true, true);
|
2022-02-15 14:46:08 +08:00
|
|
|
// handler.addConnection(1, false, 0, "", "test2--------", true, false, false, false, false);
|
|
|
|
// handler.addConnection(2, false, 0, "", "test3", true, false, false, false, false);
|
2021-03-29 15:59:14 +08:00
|
|
|
// handler.newMessage(0, 'h');
|