Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a PluginAction for toggling connection #178

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 77 additions & 39 deletions plugin/src/Components/App.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ local App = Roact.Component:extend("App")

function App:init()
self:setState({
address = "",
port = "",
sessionStatus = SessionStatus.Disconnected,
})

Expand All @@ -79,6 +81,58 @@ function App:init()
or Version.display(Config.version)
end

function App:getConnectionPair()
local address = self.state.address
if address:len() == 0 then
address = Config.defaultHost
end

local port = self.state.port
if port:len() == 0 then
port = Config.defaultPort
end

return address, port
end

function App:startSession()
local address, port = self:getConnectionPair()

Logging.trace("Starting new session")

local success, session = Session.new({
address = address,
port = port,
onError = function(message)
Logging.warn("Rojo session terminated because of an error:\n%s", tostring(message))
self.currentSession = nil

self:setState({
sessionStatus = SessionStatus.Disconnected,
})
end
})

if success then
self.currentSession = session
self:setState({
sessionStatus = SessionStatus.Connected,
})
end
end

function App:stopSession()
Logging.trace("Disconnecting session")

self.currentSession:disconnect()
self.currentSession = nil
self:setState({
sessionStatus = SessionStatus.Disconnected,
})

Logging.trace("Session terminated by user")
end

function App:render()
-- FIXME: https://github.com/Roblox/roact/issues/209
local children = {}
Expand All @@ -87,43 +141,24 @@ function App:render()
children = {
ConnectionActivePanel = e(ConnectionActivePanel, {
stopSession = function()
Logging.trace("Disconnecting session")

self.currentSession:disconnect()
self.currentSession = nil
self:setState({
sessionStatus = SessionStatus.Disconnected,
})

Logging.trace("Session terminated by user")
self:stopSession()
end,
}),
}
elseif self.state.sessionStatus == SessionStatus.ConfiguringSession then
children = {
ConnectPanel = e(ConnectPanel, {
startSession = function(address, port)
Logging.trace("Starting new session")

local success, session = Session.new({
address = address,
port = port,
onError = function(message)
Logging.warn("Rojo session terminated because of an error:\n%s", tostring(message))
self.currentSession = nil

self:setState({
sessionStatus = SessionStatus.Disconnected,
})
end
})
address = self.state.address,
port = self.state.port,

if success then
self.currentSession = session
self:setState({
sessionStatus = SessionStatus.Connected,
})
end
changeAddress = function(address)
self:setState({ address = address })
end,
changePort = function(port)
self:setState({ port = port })
end,
connect = function()
self:startSession()
end,
cancel = function()
Logging.trace("Canceling session configuration")
Expand All @@ -147,6 +182,17 @@ function App:didMount()

local toolbar = self.props.plugin:CreateToolbar("Rojo " .. self.displayedVersion)

local toggleAction = self.props.plugin:CreatePluginAction("rojo/toggle", "Rojo: Toggle connection",
"Toggles connection to a running Rojo session")

toggleAction.Triggered:Connect(function()
if self.state.sessionStatus == SessionStatus.Connected then
self:stopSession()
else
self:startSession()
end
end)

self.connectButton = toolbar:CreateButton(
"Connect",
"Connect to a running Rojo session",
Expand All @@ -156,15 +202,7 @@ function App:didMount()
checkUpgrade(self.props.plugin)

if self.state.sessionStatus == SessionStatus.Connected then
Logging.trace("Disconnecting session")

self.currentSession:disconnect()
self.currentSession = nil
self:setState({
sessionStatus = SessionStatus.Disconnected,
})

Logging.trace("Session terminated by user")
self:stopSession()
elseif self.state.sessionStatus == SessionStatus.Disconnected then
Logging.trace("Starting session configuration")

Expand Down
38 changes: 5 additions & 33 deletions plugin/src/Components/ConnectPanel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ function ConnectPanel:init()
return UDim2.new(0, container.X - other.X - 16, 0, 32)
end
)

self:setState({
address = "",
port = "",
})
end

function ConnectPanel:render()
local startSession = self.props.startSession
local cancel = self.props.cancel

return e(FitList, {
Expand Down Expand Up @@ -102,13 +96,9 @@ function ConnectPanel:render()
Input = e(FormTextInput, {
layoutOrder = 2,
width = UDim.new(0, 220),
value = self.state.address,
value = self.props.address,
placeholderValue = Config.defaultHost,
onValueChange = function(newValue)
self:setState({
address = newValue,
})
end,
onValueChange = self.props.changeAddress,
}),
}),

Expand All @@ -135,13 +125,9 @@ function ConnectPanel:render()
Input = e(FormTextInput, {
layoutOrder = 2,
width = UDim.new(0, 80),
value = self.state.port,
value = self.props.port,
placeholderValue = Config.defaultPort,
onValueChange = function(newValue)
self:setState({
port = newValue,
})
end,
onValueChange = self.props.changePort,
}),
}),
}),
Expand Down Expand Up @@ -179,21 +165,7 @@ function ConnectPanel:render()
e(FormButton, {
layoutOrder = 2,
text = "Connect",
onClick = function()
if startSession ~= nil then
local address = self.state.address
if address:len() == 0 then
address = Config.defaultHost
end

local port = self.state.port
if port:len() == 0 then
port = Config.defaultPort
end

startSession(address, port)
end
end,
onClick = self.props.connect,
}),
}),

Expand Down