private void OnGUI() { scrollPos = EditorGUILayout.BeginScrollView(scrollPos); EditorGUILayout.LabelField("Time recorder tools"); // Pause section GUILayout.Space(15); EditorGUILayout.LabelField("Pause Time recorder", EditorStyles.boldLabel); // State style var runningStateLabel = new GUIStyle(EditorStyles.boldLabel) { normal = { textColor = TimeRecorder.isPaused ? Color.red : Color.green } }; string stateLabel = TimeRecorder.isPaused ? "Paused" : "Running"; GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Time recorder is ", EditorStyles.boldLabel); EditorGUILayout.LabelField(stateLabel, runningStateLabel); // Action btn if (GUILayout.Button(TimeRecorderExtras.GetPauseButtonLabelForState(TimeRecorder.isPaused))) { ChangeTimeRecorderPauseState(!TimeRecorder.isPaused); } GUILayout.EndHorizontal(); // Backup section GUILayout.Space(15); EditorGUILayout.LabelField("Backup", EditorStyles.boldLabel); EditorGUILayout.LabelField("Generate a json file with your Time recorder work time data"); backupFileName = EditorGUILayout.TextField("Backup file name: ", backupFileName); if (GUILayout.Button("Generate backup")) { GenerateBackup(); } // Restore section GUILayout.Space(15); EditorGUILayout.LabelField("Restore from file", EditorStyles.boldLabel); EditorGUILayout.LabelField("Drag here any valid json file with the time recorder format and restore your work time"); backupTextAsset = EditorGUILayout.ObjectField("Backup file", backupTextAsset, typeof(TextAsset), false) as TextAsset; if (GUILayout.Button("Restore from backup")) { RestoreFromBackup(); } EditorGUILayout.EndScrollView(); }
/// <summary> Update visual elements from this window </summary> /// <param name="paused">is time recorder paused?</param> public void UpdatePausedState(bool paused) { TimeRecorder.isPaused = paused; var runningStateLabel = TimeRecorder.isPaused ? "Paused" : "Running"; var timeRecorderPauseStateBtn = rootVisualElement.Q <Button>("time-recorder-state-btn"); timeRecorderPauseStateBtn.text = TimeRecorderExtras.GetPauseButtonLabelForState(TimeRecorder.isPaused).ToUpperInvariant(); GetWindow <TimeRecorderWindow>().titleContent = new GUIContent($"Time recorder ({runningStateLabel})"); }
private void DrawWindow() { VisualElement root = rootVisualElement; info = TimeRecorder.LoadTimeRecorderInfoFromRegistry(); var timeRecorderTemplate = Resources.Load <VisualTreeAsset>(TimeRecorderExtras.CALENDAR_TEMPLATE_PATH); var timeRecorderTemplateStyle = Resources.Load <StyleSheet>(TimeRecorderExtras.CALENDAR_TEMPLATE_STYLE_PATH); root.styleSheets.Add(timeRecorderTemplateStyle); var dayElementTemplateStyle = Resources.Load <StyleSheet>(TimeRecorderExtras.DAY_CONTAINER_TEMPLATE_STYLE_PATH); root.styleSheets.Add(dayElementTemplateStyle); // Add tree to root element timeRecorderTemplate.CloneTree(root); // Update date label var dateLabel = root.Q <Label>(CalendarContainerTemplateNames.LABEL_DATE); dateLabel.text = $"{selectedDate.Day}-{selectedDate.Month}-{selectedDate.Year}"; // Fix buttons action var prevMonthBtn = root.Q <Button>(CalendarContainerTemplateNames.BTN_PREV_MONTH); var nextMonthBtn = root.Q <Button>(CalendarContainerTemplateNames.BTN_NEXT_MONTH); var timeRecorderPauseStateBtn = root.Q <Button>(CalendarContainerTemplateNames.TIME_RECORDER_STATE_BTN); prevMonthBtn.clicked += () => ChangeMonthOffset(-1); nextMonthBtn.clicked += () => ChangeMonthOffset(1); timeRecorderPauseStateBtn.clicked += ChangeTimeRecorderPauseState; timeRecorderPauseStateBtn.text = TimeRecorderExtras.GetPauseButtonLabelForState(TimeRecorder.isPaused).ToUpperInvariant(); // Set total label dev time var totalDevLabel = root.Q <Label>(CalendarContainerTemplateNames.LABEL_TOTAL_DEV_TIME); totalDevLabel.text = GetLabel(info?.totalRecordedTime ?? 0); // Generate days var daysContainers = new VisualElement[7]; // Get reference to all containers before generate elements for (int i = 0; i < 7; i++) { var dayElement = root.Q <VisualElement>("day-" + i); daysContainers[i] = dayElement; } int daysGenerated = 0; #region Step 1: Fill previous month var firstDay = new DateTime(selectedDate.Year, selectedDate.Month, 1); if (firstDay.DayOfWeek != DayOfWeek.Monday) { // Get previous month var previousMonthDate = selectedDate.AddMonths(-1); previousMonthDate = new DateTime(previousMonthDate.Year, previousMonthDate.Month, 1); int end = DateTime.DaysInMonth(previousMonthDate.Year, previousMonthDate.Month); int reduceDays = firstDay.DayOfWeek == DayOfWeek.Sunday ? 6 : ((int)firstDay.DayOfWeek - 1) % 7; int start = end - reduceDays; // Generate Elements FillDateToDate(daysContainers, previousMonthDate, start, end, ref daysGenerated, true); } #endregion #region Step 2: Fill selected month int daysInSelectedMonth = DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month); FillDateToDate(daysContainers, selectedDate, 0, daysInSelectedMonth, ref daysGenerated); #endregion #region Step 3: Fill remaining month days var selectedMonthFinalDate = new DateTime(selectedDate.Year, selectedDate.Month, daysInSelectedMonth); if (selectedMonthFinalDate.DayOfWeek != DayOfWeek.Sunday) { // In this calendar the final day is "Sunday" // so if selected month ends in "Sunday" there is no need to fill remaining days var nextMonth = selectedDate.AddMonths(1); int remainingDays = 7 - ((int)selectedMonthFinalDate.DayOfWeek); FillDateToDate(daysContainers, nextMonth, 0, remainingDays, ref daysGenerated, true); } #endregion }