Пример #1
0
func InterfaceStart(server ZakupkiProxyServer,
	config ServerConfig) (err error) {

	if server == nil {
		panic("InterfaceStart: passed nil server")
	}
	if config == nil {
		panic("InterfaceStart: passed nil config")
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                        INITIALIZATION                       *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	startServer := func() {
		if !server.IsRunning() {
			go func() {
				// start server
				if err := server.Start(); err != nil {
					log.Println("Cannot start server:", err)
				}
			}()
			time.Sleep(_START_SERVER_TIMEOUT)
		}
	}
	stopServer := func() {
		if server.IsRunning() {
			// shutdown server
			if err = server.ShutDown(); err != nil {
				log.Println("Cannot shutdown server", err)
			}
		}
	}
	if _RUN_SERVER_ON_STARTING && !server.IsRunning() {
		startServer()
	}
	defer func() {
		if server.IsRunning() {
			stopServer()
		}
	}()
	defer func() {
		if err = config.Save(); err != nil {
			log.Println("Cannot save configures:", err)
		}
	}()

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                      END INITIALIZATION                     *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	mw, err := walk.NewMainWindow()
	if err != nil {
		return
	}
	defer mw.Dispose()
	ni, err := walk.NewNotifyIcon()
	if err != nil {
		return
	}
	defer ni.Dispose()
	if err = ni.SetVisible(true); err != nil {
		return
	}
	if err = ni.SetToolTip(_PROG_TITLE); err != nil {
		return
	}
	ni.ShowMessage(_PROG_TITLE, _NOTICE_APP_START)
	// create image icon
	if icon, err :=
		walk.NewIconFromFile(_PROG_ICON_FILE_NAME); err != nil {
		log.Println("Cannot load icon from file:", err)
	} else {
		defer icon.Dispose()
		if err = ni.SetIcon(icon); err != nil {
			log.Println("Cannot bind img with notify icon:", err)
		}
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                            ACTIONS                          *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	startServerAction := walk.NewAction()
	err = startServerAction.SetText(_ACTION_TITLE_START_SERVER)
	if err != nil {
		return
	}
	err = startServerAction.SetVisible(!server.IsRunning())
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(startServerAction)
	if err != nil {
		return
	}

	stopServerAction := walk.NewAction()
	err = stopServerAction.SetText(_ACTION_TITLE_STOP_SERVER)
	if err != nil {
		return
	}
	err = stopServerAction.SetVisible(server.IsRunning())
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(stopServerAction)
	if err != nil {
		return
	}

	filterEnableAction := walk.NewAction()
	err = filterEnableAction.SetText(_ACTION_TITLE_FILTER_ENABLED)
	if err != nil {
		return
	}
	err = filterEnableAction.SetVisible(!config.IsFilterEnabled())
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(filterEnableAction)
	if err != nil {
		return
	}

	filterDisabledAction := walk.NewAction()
	err = filterDisabledAction.SetText(_ACTION_TITLE_FILTER_DISABLED)
	if err != nil {
		return
	}
	err = filterDisabledAction.SetVisible(config.IsFilterEnabled())
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(filterDisabledAction)
	if err != nil {
		return
	}

	removeCacheAction := walk.NewAction()
	err = removeCacheAction.SetText(_ACTION_TITLE_REMOVE_CACHE)
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(removeCacheAction)
	if err != nil {
		return
	}

	err = ni.ContextMenu().Actions().Add(walk.NewSeparatorAction())
	if err != nil {
		return
	}

	openURLGenAction := walk.NewAction()
	err = openURLGenAction.SetText(_ACTION_TITLE_OPEN_URL_GEN)
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(openURLGenAction)
	if err != nil {
		return
	}

	openDirAction := walk.NewAction()
	err = openDirAction.SetText(_ACTION_TITLE_OPEN_DIR)
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(openDirAction)
	if err != nil {
		return
	}

	openReadMeAction := walk.NewAction()
	err = openReadMeAction.SetText(_ACTION_TITLE_OPEN_README)
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(openReadMeAction)
	if err != nil {
		return
	}

	err = ni.ContextMenu().Actions().Add(walk.NewSeparatorAction())
	if err != nil {
		return
	}

	exitAction := walk.NewAction()
	err = exitAction.SetText(_ACTION_TITLE_EXIT)
	if err != nil {
		return
	}
	err = ni.ContextMenu().Actions().Add(exitAction)
	if err != nil {
		return
	}

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                        EVENT HANDLERS                       *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	updateServerButtons := func() {
		if server.IsRunning() {
			if err = startServerAction.SetVisible(false); err != nil {
				log.Println(err)
			}
			if err = stopServerAction.SetVisible(true); err != nil {
				log.Println(err)
			}
		} else {
			if err = stopServerAction.SetVisible(false); err != nil {
				log.Println(err)
			}
			if err = startServerAction.SetVisible(true); err != nil {
				log.Println(err)
			}
		}
	}

	startServerAction.Triggered().Attach(func() {
		if !server.IsRunning() {
			if err = startServerAction.SetEnabled(false); err != nil {
				log.Println(err)
			}
			startServer()
			if server.IsRunning() {
				ni.ShowMessage(_PROG_TITLE, _NOTICE_PROXY_ENABLED)
			}
			if err = startServerAction.SetEnabled(true); err != nil {
				log.Println(err)
			}
		}
		updateServerButtons()
	})

	stopServerAction.Triggered().Attach(func() {
		if server.IsRunning() {
			stopServer()
			ni.ShowMessage(_PROG_TITLE, _NOTICE_PROXY_DISABLED)
		}
		updateServerButtons()
	})

	updateFilterButtons := func() {
		if config.IsFilterEnabled() {
			err = filterEnableAction.SetVisible(false)
			if err != nil {
				log.Println(err)
			}
			err = filterDisabledAction.SetVisible(true)
			if err != nil {
				log.Println(err)
			}
		} else {
			err = filterDisabledAction.SetVisible(false)
			if err != nil {
				log.Println(err)
			}
			err = filterEnableAction.SetVisible(true)
			if err != nil {
				log.Println(err)
			}
		}
	}

	filterEnableAction.Triggered().Attach(func() {
		if !config.IsFilterEnabled() {
			config.SetFilterEnabled(true)
			ni.ShowInfo(_PROG_TITLE, _NOTICE_ENABLED_FILTERS)
		}
		updateFilterButtons()
	})

	filterDisabledAction.Triggered().Attach(func() {
		if config.IsFilterEnabled() {
			config.SetFilterEnabled(false)
			ni.ShowInfo(_PROG_TITLE, _NOTICE_DISABLED_FILTERS)
		}
		updateFilterButtons()
	})

	removeCacheAction.Triggered().Attach(func() {
		if err = server.RemoveCache(); err != nil {
			log.Println("Cannot remove cache:", err)
		} else {
			ni.ShowInfo(_PROG_TITLE, _NOTICE_CACHE_REMOVED)
		}
	})

	openURLGenAction.Triggered().Attach(func() {
		err = exec.Command(_PROG_URL_GENER_FILE_NAME).Start()
		if err != nil {
			log.Println("Cannot open url generator:", err)
		}
	})

	openDirAction.Triggered().Attach(func() {
		err = exec.Command("cmd", "/C", "start", ".").Start()
		if err != nil {
			log.Println("Cannot open program directory:", err)
		} else {
			ni.ShowInfo(_PROG_TITLE, _NOTICE_CONFIGS)
		}
	})

	openReadMeAction.Triggered().Attach(func() {
		err = exec.Command(
			"cmd", "/C", "start", _PROG_DESCRIPTION_FILE_NAME,
		).Start()
		if err != nil {
			log.Println("Cannot open README:", err)
		}
	})

	exitAction.Triggered().Attach(func() {
		walk.App().Exit(0)
	})

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *                       END EVENT HANDLERS                    *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	mw.Run()

	return
}
Пример #2
0
func (this *Table) AddSeparatorAction() *Table {
	act := walk.NewSeparatorAction()
	this.ContextMenu().Actions().Add(act)

	return this
}
Пример #3
-1
func (s Separator) createAction(builder *Builder, menu *walk.Menu) (*walk.Action, error) {
	action := walk.NewSeparatorAction()

	if menu != nil {
		if err := menu.Actions().Add(action); err != nil {
			return nil, err
		}
	}

	return action, nil
}